Can I custom the url that be automatically opened in browser? I found there is no api for that? Since that there is not a index under the project root, but the default url is localhost:8080. Or in the condition that I want to debug the page being developing.
-
If you need to change the host, set the `host` property. More here - https://webpack.js.org/configuration/dev-server/#devserverhost – Lucas Jan 22 '21 at 15:55
6 Answers
September 2021 (webpack 5)
From the documentation: https://webpack.js.org/configuration/dev-server/#devserveropen
In the dev server config
{
...,
host: '0.0.0.0',
open: ['http://locahost:3001']
...
}
(and remove the --open
flag from the webpack serve
command

- 8,458
- 13
- 59
- 133
-
-
Thanks removing the --open flag was key issue for me. I had --open flag on npm start command which seems to override the config file. – Firze Mar 16 '23 at 07:28
You can configure you webpack.config.js like this:
entry: __dirname + '/src/index.js',
output: {
path: path.join(__dirname, 'static'),,
filename: "bundle.js",
publicPath: "/static/dist/"
},
devServer: {
publicPath: '/static/dist/',
open: true,
openPage: 'static/dist/somefile.html'
},...
The important thing here is devServer.open and devServer.openPage. You have to enable devServer.open and set your custom url in devServer.openPage to open first in your devServer. My configuration in package.json:
"scripts": {
"start": "webpack-dev-server --mode development --open --hot",
"build": "webpack --mode production",
"dev": "webpack --mode development"
},...
Finally run it by:
npm run start
Hope it helps. Regards

- 617
- 7
- 7
You can try with this plugin: Open Browser Webpack Plugin
Follow this steps...
First of all install the plugin:
npm install open-browser-webpack-plugin --save-dev
Remove the
--open
option from package.json or theopen: true
option from webpack.config.js, devServer configuration or theopen: true
in your webpack.config.jsNow you need to require and configure the plugin into webpack.config.js
var OpenBrowserPlugin = require('open-browser-webpack-plugin');` module.exports = { ... ... ... plugins: [ new OpenBrowserPlugin({ url: 'http://localhost:3000/mycustompath' }) ] };

- 1,118
- 11
- 22
my solution without any plugins
modify your
package.json
file, like below code shows
- macOS / Linux
{
"scripts": {
"dev": "open http://localhost:8080 && webpack --config webpack.config.js",
}
}
- Windows
{
"scripts": {
"dev": "start http://localhost:8080 && webpack --config webpack.config.js",
}
}
- webpack-dev-server
{
"scripts": {
"dev": "webpack-dev-server --open --config webpack.config.js",
}
}
refs
https://webpack.js.org/configuration/dev-server/
https://webpack.js.org/guides/development/#using-webpack-dev-server

- 10,077
- 1
- 69
- 68
To open a specified page in a browser:
webpack.config.js
module.exports = {
//...
devServer: {
open: ['/my-page'],
},
};
Usage via the CLI:
npx webpack serve --open /my-page
You can find this section https://webpack.js.org/configuration/dev-server/

- 99
- 2
- 3