简体   繁体   English

为什么npm直接将cmd行标志传递给我的脚本?

[英]Why is npm passing cmd line flags directly to my script?

I'm using npm to run a build and I'm trying to override my .npmrc config options using cmd line flags. 我正在使用npm运行构建,并且尝试使用cmd线标志覆盖我的.npmrc配置选项。 However npm insists on passing these flags directly to my script rather than reading them as config options as described in the docs. 但是npm坚持将这些标志直接传递给我的脚本,而不是按照文档中的描述将其作为配置选项读取。 What am I doing wrong? 我究竟做错了什么?

From the cmd line I try to build a Tizen pacakge like so: 从cmd行,我尝试构建一个Tizen pacakge,如下所示:

npm run package --tizen_profile myprofile

inside my package.json I have: 在我的package.json中,我有:

"package": "tizen package -t wgt --sign $npm_package_config_tizen_profile -- .buildResult/wgt -o .buildResult/wgt"

The result from running the command is: 运行命令的结果是:

package: `tizen package -t wgt --sign $npm_package_config_tizen_profile -- .buildResult/wgt -o .buildResult/wgt "myprofile"`

Where it should be: 应该在哪里:

package: `tizen package -t wgt --sign "myprofile"_tizen_profile -- .buildResult/wgt -o .buildResult/wgt`

It's like npm is merely appending the cmd line argument to the script command instead of plugging it in like a variable as described in the docs: https://docs.npmjs.com/misc/config 就像npm只是将cmd line参数附加到脚本命令,而不是像docs中所述的变量一样将其插入: https : //docs.npmjs.com/misc/config

Has there been a recent update to npm which deprecates and removes this ability? 是否有npm的最近更新,它已弃用并删除了此功能? I just updated to npm 6.x but it was working this way on 5.x as well. 我刚刚更新到npm 6.x,但在5.x上也是如此。

you can try to rewrite your script within package.json without --sign flag like: 您可以尝试在没有--sign标志的情况下在package.json重写脚本,例如:

"package": "tizen package -t wgt -- .buildResult/wgt -o .buildResult/wgt"

and then pass it when you run npm command: 然后在运行npm命令时将其传递:

npm run package -- --sign myprofile

I assume that you can change the order of arguments, because --sign myprofile now will be at the very end of your command 我假设您可以更改参数的顺序,因为--sign myprofile现在将在命令的最后

UPDATE 更新

here is another way to pass variables and place them in any place in your CLI command (without using npm config set ). 这是传递变量并将其放置在CLI命令中任何位置的另一种方法(不使用npm config set )。 In my package.json : 在我的package.json

"aaa": "ls $myoptionalflag && $mycmd"

this way I can pass any flag to la command (or not to pass at all) and I can pass any CLI command as mycmd variable. 这样,我可以将任何标志传递给la命令(或根本不传递),并且可以将任何CLI命令作为mycmd变量传递。 So, now I can run: 因此,现在我可以运行:

myoptionalflag=-la mycmd=pwd npm run aaa

which will execute 将执行

ls -la && pwd

or 要么

mycmd=pwd npm run aaa

which will execute 将执行

ls && pwd

I FIGURED IT OUT! 我想到了!

The documentation is flawed as it doesn't tell you the correct syntax for passing npm config properties. 该文档有缺陷,因为它没有告诉您传递npm config属性的正确语法。 I had to use: 我不得不使用:

npm run package --mypackagename:tizen_profile="myprofile"

where mypackagename is the name property used in package.json. 其中mypackagename是package.json中使用的name属性。 Also note the key value syntax is --key=value and not --key value as described in the docs. 还要注意,键值语法是--key=value而不是文档中介绍的--key value Again, --key would be packagename:key using the name specified at the top level of your package.json . 同样,-- --key将是packagename:key,使用在package.json顶层指定的名称。

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

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