Is there a way to pass arguments inside of the package.json command?
My script:
"scripts": {
"test": "node mytest.js $1 $2 | node_modules/tap-difflet/bin/tap-difflet"
}
cli npm run test 8080 production
Then on mytest.js
I'd like to get the arguments with process.argv
Passing arguments to script
To pass arguments to npm script, you should provide them after
--
for safety.In your case,
--
can be omitted. They behaves the same:But when the arguments contain option(s) (e.g.
-p
),--
is necessary otherwise npm will parse them and treat them as npm's option.Use positional parameters
The arguments are just appended to the script to be run. Your
$1
$2
won't be resolved. The command that npm actually runs is:In order to make position variable works in npm script, wrap the command inside a shell function:
Or use the tool scripty and put your script in an individual file.
package.json:
scripts/test: