javascript npm 参数导致“参数数量不足或找不到条目”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50740947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:15:19  来源:igfitidea点击:

npm argument results in "Insufficient number of arguments or no entry found"

javascriptnode.jsnpmwebpack

提问by Perneel

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

我尝试将自定义标志从 npm 脚本传递到我的 webpack 配置,但导致以下错误。日志

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

包.json

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

webpack.config.js

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
  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
  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
  1. npm run dev

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

有趣的是,我尝试运行带有--no-dist标志的开发脚本。输出:

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

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

如您所见,no_dist布尔值设置为 true,这是想要的行为。但我收到以下错误:

Insufficient number of arguments or no entry found.
Alternatively, run 'webpack(-cli) --help' for usage info.
  1. npm run dev --user test
  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.

与测试 3 的行为相同。参数传递给 webpack.config.js 但导致相同的错误。

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


Am I missing something here?

我在这里错过了什么吗?

回答by Perneel

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

正如@squgeim 提到的,webpack 不支持标志,应该使用环境变量。这为我指明了正确的方向:环境变量

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

我像这样更改了我的 package.json 和 webpack.config.js 文件:

package.json

包.json

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

--env.dist=falseadds distto the environment variables.

--env.dist=false添加dist到环境变量中。

webpack.config.js

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.

您必须对 webpack 配置进行一项更改。通常,module.exports 指向配置对象。要使用 env 变量,您必须将 module.exports 转换为函数。

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!

这似乎是正确的做法。再次感谢@squgeim 为我指出正确的方向!

回答by squgeim

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

我不认为 webpack 在它的配置中支持自定义 cli 标志。

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') {
}

回答by Anoman.M

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.exportinstead of module.exportsin webpack.config.js.

我刚刚得到了同样的错误yesturday,我发现在我的情况下,它是由于使用了ES6语法error.I module.export,而不是module.exportswebpack.config.js