gpt4 book ai didi

javascript - npm 参数导致 "Insufficient number of arguments or no entry found"

转载 作者:搜寻专家 更新时间:2023-10-31 23:30:22 26 4
gpt4 key购买 nike

我尝试将自定义标志从 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! 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

...
"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);

测试

  1. npm run dev:dist

这工作没有错误,它可以毫无问题地捆绑和分发。给出以下输出:

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

同样有效并给出以下输出:

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

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

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

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

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

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

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

我是不是漏掉了什么?

最佳答案

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

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

package.json

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

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

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
}

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

关于javascript - npm 参数导致 "Insufficient number of arguments or no entry found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50740947/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com