简体   繁体   English

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

[英]npm argument results in “Insufficient number of arguments or no entry found”

I try to pass a custom flag from npm script to my webpack config but it results in following error. 我尝试将自定义标志从npm脚本传递到我的webpack配置,但它会导致以下错误。 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! my-project@4.0.0 dev: `webpack --mode development -- --no-dist`
npm ERR! Exit status 2
npm ERR!
npm ERR! Failed at the my-project@4.0.0 dev script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

package.json 的package.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

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. 这里有趣,我尝试运行带有--no-dist标志的开发脚本。 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. 如您所见, no_dist布尔值设置为true,这是想要的行为。 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. 与测试3相同的行为。参数传递给webpack.config.js但导致相同的错误。

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

Am I missing something here? 我在这里错过了什么吗?

As @squgeim mentioned, webpack does not support flags and environment variables should be used. 正如@squgeim所提到的,webpack不支持标志,应该使用环境变量。 This pointed me in the right direction: Environment variables 这使我指出了正确的方向: 环境变量

I changed my package.json and webpack.config.js file like this: 我改变了我的package.json和webpack.config.js文件,如下所示:

package.json 的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. --env.dist=falsedist添加到环境变量中。

webpack.config.js webpack.config.js

There is one change that you will have to make to your webpack config. 您必须对webpack配置进行一项更改。 Typically, module.exports points to the configuration object. 通常,module.exports指向配置对象。 To use the env variable, you must convert module.exports to a function. 要使用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指出我正确的方向!

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM