I'd like to pass an argument into the first of two commands in my npm script:
"scripts": {
"myscript": "a && b"
}
When I run npm run myscript -- --somearg=somevalue
, the argument is passed to command b
but not command a
. Is there some way to instead ensure that the first command receives the argument?
"scripts": {
"myscript": "a",
"postmyscript": "b"
}
I found the following solution after searching a lot from this discussion:
https://github.com/npm/npm/issues/9627#issuecomment-152607809
and
https://github.com/npm/npm/issues/9627#issuecomment-178576802
Solution 1. So in your package json add a config object like so
{
"name": "packageName",
"config" : { "variable1" : "value1" }
}
then you can access the variable like so on windows
{
"name": "packageName",
"config" : { "variable1" : "value1" },
"scripts": {
"myscript": "a -c %npm_package_config_variable1% && b -c %npm_package_config_variable1%"
}
}
in mac/linux I am assuming (not a 100%)
{
"name": "packageName",
"config" : { "variable1" : "value1" },
"scripts": {
"myscript": "a -c $npm_package_config_variable1 && b -c $npm_package_config_variable1"
}
}
then you can override the variable by calling the script like so:
npm run myscript --packageName:variable1=value2
Solution 2.
no need for the config entry in the package.json
{
"name": "packageName",
"scripts": {
"myscript": "a -c %npm_config_variable1% && b -c %npm_config_variable1%"
}
}
and call it like so
npm run myscript --variable1=value2
reason to pick one over the other:
https://github.com/npm/npm/issues/9627#issuecomment-178813965
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With