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'
}
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.
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}`,
},
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With