Max Heyer
Max Heyer

Reputation: 135

webpack-dev-server dynamic port

Is it possible to find a unused port for webpack-dev-server? My current configuration does look like:

devServer: {
    historyApiFallback: true,
    inline: true,
    host: '0.0.0.0',
    port: 3000,
    contentBase: helpers.root('public'),
    stats: 'minimal'
}

Upvotes: 3

Views: 3841

Answers (2)

kimbaudi
kimbaudi

Reputation: 15555

If omitting port is not an option, you can use portfinder-sync to automatically select the next available port for you:

const portFinderSync = require('portfinder-sync')

devServer: {
    historyApiFallback: true,
    inline: true,
    host: '0.0.0.0',
    port: portFinderSync.getPort(3000),
    contentBase: helpers.root('public'),
    stats: 'minimal'
}

In my case, I couldn't omit port because I needed it to set public property in my devServer config:

const portFinderSync = require('portfinder-sync')
const port = portFinderSync.getPort(8080)

devServer: {
  contentBase: path.join(__dirname, 'dist'),
  host: '0.0.0.0',
  open: true,
  port: port,
  public: `${ipaddress}:${port}`,
},

Upvotes: 2

simon04
simon04

Reputation: 3302

When omitting port, webpack-dev-server uses the first available port starting with 8000, see the PR. Requiring webpack-dev-server⩾2.2.

For earlier versions, the port 0 trick could work. See here for details.

Upvotes: 7

Related Questions