Sending command line args to npm script
May 9, 2022
⋅
1 minutes read
Today I learned how to pass arguments to the npm scripts. My current scripts look like this:
I can simply run node ./build.js
by running npm run build
to build my code. However, I would like to be able to run something like node ./build.js --watch
which means I want to pass argument(s) to my npm script.
I've tried npm build --watch
but the script is end up with just node ./build.js
.
After googling. I find that I need to use --
separator to separate the params passed to npm
command itself and the params passed to my script.
The syntax is as follows:
Here's how I pass arguments to my script.
If your params do not start with -
or --
, then having an explicit --
is not needed. For examples:
However, it's better to use the separator anyway for clarify.