I am trying to pass argument in my npm command and use that argument in my script
Example:
npm run test -b chrome -e QA
"scripts": {
"test": "something.js ./xyz/abc/cdf --something \"{\\\"browser\\\": \\\"<process.argv[2]>\\\"}\""
}
I am not sure, how to access in my script.
Please advice
In something.js you can access the process arguments by process.argv.
It will be an array of x elements, where the first two are the executable running your script and the second is a path to the script that is being ran.
Try console.log(process.argv); to see whats up.
In your specific example you should remove the escaped " characters to get it working, like so:
running node in terminal
node somethings.js ./xyz/abc/cdf --something "{\\\"browser\\\": \\\"<process.argv[2]>\\\"}"
Results in:
[ '/usr/local/bin/node',
'/Users/user/Documents/test.js',
'./xyz/abc/cdf',
'--something',
'{\\"browser\\": \\"<process.argv[2]>\\"}' ]
package.json script
"scripts": {
"test": "node test.js"
},
Note: add node as the executable in the test script
Running npm run test -b chrome -e QA
Results in:
[ '/usr/local/bin/node',
'/Users/user/Documents/test.js',
'chrome',
'QA' ]
If you'd like to get the -b and -e arguments in there too, add --. Like so:
npm run test -- -b chrome -e QA
Results in
[ '/usr/local/bin/node',
'/Users/user/Documents/test.js',
'-b',
'chrome',
'-e',
'QA' ]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With