How can I determine if webpack.config.js
was loaded via webpack
vs webpack-dev-server
?

- 272,448
- 266
- 850
- 1,236
5 Answers
Update: The environment variable is changed to WEBPACK_SERVE
.
The webpack dev server will now set the WEBPACK_DEV_SERVER
environment variable, allowing for a more robust way to check.
const isDevServer = process.env.WEBPACK_DEV_SERVER;

- 3,118
- 2
- 22
- 36

- 405
- 6
- 11
-
1Both of these are undefined for me in Webpack 5.58.1 with `devServer` enabled in the config. – JoshuaCWebDeveloper Nov 18 '22 at 17:17
Either:
const isDevServer = process.argv[1].indexOf('webpack-dev-server') !== -1;
or:
const isDevServer = process.argv.some(v => v.indexOf('webpack-dev-server') !== -1);
or:
const isDevServer = process.argv.some(v => v.includes('webpack-dev-server'));
I've been using the latter to a great effect. One configuration FTW!

- 11,168
- 4
- 52
- 67
-
or... `const isDevServer = process.argv.find(v => v.includes('webpack-dev-server'))` – Alex Gyoshev Aug 05 '16 at 14:15
-
2One nitpick: the 2nd and 3rd options will actually set `isDevServer` to the value of the `argv` element that contained `'webpack-dev-server'`. Substitute `some` for `find` to ensure `isDevServer` is a boolean. – lawnsea Mar 21 '18 at 19:18
-
1@lawnsea just seen this and you're right, `some` would be better. I've edited my answer and thank you :) – bjfletcher May 09 '20 at 13:58
I found one potential soluton:
var isDevServer = path.basename(require.main.filename) === 'webpack-dev-server.js';
-
1I like this answer more. Alternatively you can do: `path.relative('', require.main.filename) == 'node_modules/webpack-dev-server/bin/webpack-dev-server.js'`. – x-yuri Nov 11 '17 at 18:28
Or use the Webpack Environment Variables:
// The webpack command line
webpack-dev-server --open --env.devServer
// webpack.config.js
module.exports = env => {
console.log('isDevServer: ', env.devServer) // true
...
}

- 111
- 1
- 5
-
can you leverage that so that you only start the webpack dev server IF it's not already started? – james emanon Sep 22 '20 at 20:53
In general, webpack-dev-server
runs with mode='development'
(https://webpack.js.org/configuration/mode/#root), while webpack
runs with mode=production
. If so, you can use the value of:
process.env.NODE_ENV