DEV Community

Cover image for Handling Command-line Arguments in NPM Scripts

Handling Command-line Arguments in NPM Scripts

Felippe Regazio on December 09, 2020

Custom Arguments Lets imagine that you have a NPM script to lint your application. Something like that: "scripts": { "lint": "eslin...
Collapse
 
akhilgautam profile image
Akhil

Nicely explained but I contradict on the usage of last set of commands. The sole reason to write four different scripts is to reduce the pain of typing npm run serve --stage=development every time even in dev environment. Just my perception, you might have different views on it.

Collapse
 
felipperegazio profile image
Felippe Regazio • Edited

thats a fair point. in this case, i would prefer to add a new "serve:dev" with the proper stage flag. but I think it did its job as an example (?) :P another way would be to infer the 'development' mode on your application when there is no flag.

Collapse
 
mariopetrovic profile image
Mario Petrovic

This is great. Thanks for pointing to this npm feature.

I have a question, if i want to have argument that is purely a flag, how can i pass it to npm script. Example:

npm test --coverage
Enter fullscreen mode Exit fullscreen mode

I tried this and it didnt work:

"test": "react-scripts test --coverage=$npm_config_coverage"
Enter fullscreen mode Exit fullscreen mode

Is there a way to pass boolean type of flag to npm script?

NOTE:
I dont want to use -- delimiter to pass arguments as it is required for npm.

Collapse
 
peanutqaq profile image
PeanutQAQ

hi there. i have the same question. have you sovled this question? would you perfer to share me your solution?

Collapse
 
mariopetrovic profile image
Mario Petrovic

Hey. No luck on my side. I did not look anywhere else on this topic. This was mostly my curiosity rather than real life need.
If i stumble upon anything i will post it here.

Collapse
 
pierbjx profile image
PierBJX

Hello,
this does not work on Windows env. How could I make it work ?

Collapse
 
felipperegazio profile image
Felippe Regazio

Im really sorry, but i have only Unix-Like Systems at hand now, so i cant help you since i have no way to test it on a windows system. But you can take a look on the NPM official documentation:

docs.npmjs.com/cli/v7/using-npm/co...

It doesn't tell anything about config variables specificity for windows, so maybe you can do a review on your scripts following the docs to do a double check.

Collapse
 
zorida profile image
zorida

Try to install cross-env as a dev dependency. Then you have a universal way to declare scripts with parameters / arguments. :)

npmjs.com/package/cross-env

Collapse
 
dorklord23 profile image
Tri R.A. Wibowo

For people using PNPM, this doesn't work with it. You must use NPM to have the named param works. Not sure about yarn tho.

Collapse
 
alysonbasilio profile image
Alyson Fernandes Basilio

I've always thought that existed a way of doing things like that but never read the docs to see how to use these features. Thanks for the great article, I absolutely will use this for now on.

Collapse
 
soheilss profile image
SoHeil-SS

npm run serve --stage=development ? uh no no no
npm run serve:dev ? not even this...
i make a alias for them on linux
for example:
alias nrs:d='npm run serve:dev'

easy 😂