Why is webpack-dev-server throwing an error when I

2019-08-22 17:34发布

问题:

This is my script:

"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js --arg devus"

In my old setup I used to do:

"dev": "node build/dev-server.js --arg dev"

How can I achieve the same (having the --arg dev thing) with webpack-dev-server?

This is the error:

Unknown argument: arg
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! test-phantom@1.0.0 dev: `webpack-dev-server --inline --progress --config build/webpack.dev.conf.js --arg devus`

回答1:

The thing you described was possible with webpack 1.

As of webpack 2+ (and webpack-dev-server will pass the arguments to the webpack as is) passing the custom arguments can be done only by prefixing the arguments with env.:

"dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js --env.arg devus"

However your script will not work as is, you would need to adapt it to use env property inside of your webpack configuration.

In other words this is how your config should look like

module.exports = env => {
  console.log(env);

  return {
    // your config heree
  }
};

See the documentation