Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Multiple Arguments in npm command

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

like image 678
Frontend developer Avatar asked Dec 07 '25 18:12

Frontend developer


1 Answers

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' ]

like image 50
Alex Avatar answered Dec 09 '25 07:12

Alex