javascriptnode.jsparametersnpmrun-script

named parameter to npm run script


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)


Solution

  • 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