Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 441
Ben Avatar asked Aug 06 '15 19:08

Ben


People also ask

How do you pass command line arguments in npm script?

The -- notation tells your script to pass the parameters to the current command invoked by NPM. From the NPM docs: "NPM will pass all the arguments after the -- directly to your script".

How do I run a script defined package in json?

To define an NPM script, set its name and write the script under the 'scripts' property of your package. json file: To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command. Some predefined aliases convert to npm run, like npm test or npm start, you can use them interchangeably.

What is npm Run command?

npm run sets the NODE environment variable to the node executable with which npm is executed. If you try to run a script without having a node_modules directory and it fails, you will be given a warning to run npm install , just in case you've forgotten.


2 Answers

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

like image 86
robertklep Avatar answered Oct 19 '22 23:10

robertklep


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"
like image 34
Kentonbmax Avatar answered Oct 20 '22 00:10

Kentonbmax