最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - npm argument results in "Insufficient number of arguments or no entry found" - Stack Overflow

programmeradmin3浏览0评论

I try to pass a custom flag from npm script to my webpack config but it results in following error. The logs

Insufficient number of arguments or no entry found.
Alternatively, run 'webpack(-cli) --help' for usage info.

ERROR in Entry module not found: Error: Can't resolve '--no-dist' in 'C:\Users\user\gitroot\MyProject\sharepoint'
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] dev: `webpack --mode development -- --no-dist`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

package.json

...
"scripts": {
    "dev": "webpack --mode development -- --no-dist",
    "dev:dist": "webpack --mode development",
    "build": "webpack --mode production"
},
...

webpack.config.js

let username = process.env.USERNAME;
if (process.env.npm_config_user !== undefined && process.env.npm_config_user !== "") {
    username = process.env.npm_config_user;
}
console.log("username", username);
console.log("process.argv.slice(2)", process.argv.slice(2));
const no_dist = process.argv.slice(2).indexOf("--no-dist") > -1;
console.log("no_dist", no_dist);

Tests

  1. npm run dev:dist

This works without errors, it gets bundled and distributed without problems. Gives the following output:

username user
process.argv.slice(2) [ '--mode', 'development' ]
no_dist false
  1. npm run dev:dist --user test

Also works and gives the following output:

username test
process.argv.slice(2) [ '--mode', 'development' ]
no_dist false
  1. npm run dev

Here it gets interesting, I try to run the dev script which has a --no-dist flag. Output:

username user
process.argv.slice(2) [ '--mode', 'development', '--', '--no-dist' ]
no_dist true

As you can see, no_dist boolean is set to true, which is the wanted behaviour. But I get the following error:

Insufficient number of arguments or no entry found.
Alternatively, run 'webpack(-cli) --help' for usage info.
  1. npm run dev --user test

Same behaviour as test 3. The arguments are passed to the webpack.config.js but result in the same error.

username test
process.argv.slice(2) [ '--mode', 'development', '--', '--no-dist' ]
no_dist true

Am I missing something here?

I try to pass a custom flag from npm script to my webpack config but it results in following error. The logs

Insufficient number of arguments or no entry found.
Alternatively, run 'webpack(-cli) --help' for usage info.

ERROR in Entry module not found: Error: Can't resolve '--no-dist' in 'C:\Users\user\gitroot\MyProject\sharepoint'
npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! [email protected] dev: `webpack --mode development -- --no-dist`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the [email protected] dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

package.json

...
"scripts": {
    "dev": "webpack --mode development -- --no-dist",
    "dev:dist": "webpack --mode development",
    "build": "webpack --mode production"
},
...

webpack.config.js

let username = process.env.USERNAME;
if (process.env.npm_config_user !== undefined && process.env.npm_config_user !== "") {
    username = process.env.npm_config_user;
}
console.log("username", username);
console.log("process.argv.slice(2)", process.argv.slice(2));
const no_dist = process.argv.slice(2).indexOf("--no-dist") > -1;
console.log("no_dist", no_dist);

Tests

  1. npm run dev:dist

This works without errors, it gets bundled and distributed without problems. Gives the following output:

username user
process.argv.slice(2) [ '--mode', 'development' ]
no_dist false
  1. npm run dev:dist --user test

Also works and gives the following output:

username test
process.argv.slice(2) [ '--mode', 'development' ]
no_dist false
  1. npm run dev

Here it gets interesting, I try to run the dev script which has a --no-dist flag. Output:

username user
process.argv.slice(2) [ '--mode', 'development', '--', '--no-dist' ]
no_dist true

As you can see, no_dist boolean is set to true, which is the wanted behaviour. But I get the following error:

Insufficient number of arguments or no entry found.
Alternatively, run 'webpack(-cli) --help' for usage info.
  1. npm run dev --user test

Same behaviour as test 3. The arguments are passed to the webpack.config.js but result in the same error.

username test
process.argv.slice(2) [ '--mode', 'development', '--', '--no-dist' ]
no_dist true

Am I missing something here?

Share Improve this question edited Jun 8, 2018 at 9:36 Perneel asked Jun 7, 2018 at 12:13 PerneelPerneel 3,3677 gold badges49 silver badges69 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

As @squgeim mentioned, webpack does not support flags and environment variables should be used. This pointed me in the right direction: Environment variables

I changed my package.json and webpack.config.js file like this:

package.json

"scripts": {
    "dev": "webpack --mode development --env.dist=false",
    "dev:dist": "webpack --mode development",
    "build": "webpack --mode production"
},

--env.dist=false adds dist to the environment variables.

webpack.config.js

There is one change that you will have to make to your webpack config. Typically, module.exports points to the configuration object. To use the env variable, you must convert module.exports to a function.

module.exports = env => {
    const no_dist = (env && env.dist === "false");

return {
    //webpack configuration
}

This seems to be the correct way to do it. Thanks again @squgeim to point me in the right direction!

I just got the same error yesturday,and i found out in my case it is due to the ES6 syntax error.I used module.export instead of module.exports in webpack.config.js.

I don't think webpack supports custom cli flags in it's configurations.

The idiomatic approach is to use environment variables to pass custom arguments to your configurations.

"dev:dist": "DIST=true webpack --mode development"

And access it like:

if (process.env.DIST === 'true') {
}
发布评论

评论列表(0)

  1. 暂无评论