问题
I would like to pass a named parameter to an npm run script so I can do something like the following:
"scripts":{
"say-hello":"echo $greeting && ls"
}
npm run hello --greeting=hello
I'd like it to then put 'hello' in place of the $greeting variable, echo the command and then do the ls (this is obviously just a simple example of a chained command)
回答1:
Just found out that this works:
"scripts":{
"say-hello" : "echo $npm_config_greeting && ls"
}
Edit:
Any environment variables that start with npm_config_ will be interpreted as a configuration parameter. For example, putting npm_config_foo=bar in your environment will set the foo configuration parameter to bar.
npm docs
回答2:
Another way is to simply add the following '--' followed by your desired param(s). This would be for named parameters that '=' a value. Please note the underlying process in this example, a protractor call, expects a Url arg.
npm run e2e -- --Url="https://yoururlhere.com"
来源:https://stackoverflow.com/questions/31863828/named-parameter-to-npm-run-script