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

163 Views Asked by At

I'm using npm to run a build and I'm trying to override my .npmrc config options using cmd line flags. However npm insists on passing these flags directly to my script rather than reading them as config options as described in the docs. What am I doing wrong?

From the cmd line I try to build a Tizen pacakge like so:

npm run package --tizen_profile myprofile

inside my package.json I have:

"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

Has there been a recent update to npm which deprecates and removes this ability? I just updated to npm 6.x but it was working this way on 5.x as well.

2

There are 2 best solutions below

1
On BEST ANSWER

I FIGURED IT OUT!

The documentation is flawed as it doesn't tell you the correct syntax for passing npm config properties. I had to use:

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

where mypackagename is the name property used in package.json. Also note the key value syntax is --key=value and not --key value as described in the docs. Again, --key would be packagename:key using the name specified at the top level of your package.json.

2
On

you can try to rewrite your script within package.json without --sign flag like:

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

and then pass it when you run npm command:

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

UPDATE

here is another way to pass variables and place them in any place in your CLI command (without using npm config set). In my 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. 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