Skip to content

listeningApp is defined late and cannot be used in before/after functions #1569

Closed
@cinderblock

Description

@cinderblock
  • 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
  • This is a bug
  • This is a modification request

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);
      });
    },
  },
  // ...
};

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions