Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
}
like image 342
Max Heyer Avatar asked Feb 18 '26 06:02

Max Heyer


2 Answers

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.

like image 189
simon04 Avatar answered Feb 19 '26 18:02

simon04


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}`,
},
like image 44
kimbaudi Avatar answered Feb 19 '26 18:02

kimbaudi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!