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