- Operating System:
Windows 10
- Node Version:
v8.11.2
- NPM Version:
5.6.0
- webpack Version:
4.25.1
- webpack-dev-server Version:
3.1.10
Code
Fails: Cannot read property 'address' of undefined
// webpack.config.js
module.exports = {
devServer: {
port: 9001,
host: '0.0.0.0',
before(app, server) {
const port = server.listeningApp.address().port;
console.log('Listening on port:', port);
},
},
// ...
};
Yes, in this simplistic example, I recognize that I could hard code or add a variable port that is shared between the devServer configuration object and my before function. However I'm trying to support other cases where this is not practical.
Problem
listeningApp is set after before function is executed.
#476 Almost addressed this, however creating the server instance is not controllable in the features list and is always done last.
I think it would be a relatively small change in the startup sequence to add creating the server to the features list and allowing it to be controlled. I don't see how it would affect other parts of the code. If this change is undesirable, I still have a workaround that works for me but I bet others might like this change aswell.
Workaround
Use setImmediate() to delay execution inside before function. This happens to work for my particular use case but this introduces other annoyances.
// webpack.config.js
module.exports = {
devServer: {
port: 9001,
host: '0.0.0.0',
before(app, server) {
setImmediate(() => {
const port = server.listeningApp.address().port;
console.log('Listening on port:', port);
});
},
},
// ...
};
Windows 10v8.11.25.6.04.25.13.1.10Code
Fails:
Cannot read property 'address' of undefinedYes, in this simplistic example, I recognize that I could hard code or add a variable
portthat is shared between thedevServerconfiguration object and mybeforefunction. However I'm trying to support other cases where this is not practical.Problem
listeningAppis set afterbeforefunction is executed.#476 Almost addressed this, however creating the server instance is not controllable in the
featureslist and is always done last.I think it would be a relatively small change in the startup sequence to add creating the server to the
featureslist and allowing it to be controlled. I don't see how it would affect other parts of the code. If this change is undesirable, I still have a workaround that works for me but I bet others might like this change aswell.Workaround
Use
setImmediate()to delay execution insidebeforefunction. This happens to work for my particular use case but this introduces other annoyances.