Make a script which accept command-line arguments


Question

What is the correct syntax for running a Node.js script with command-line arguments on Linux or Mac?

To run the script with no arguments, I would simply use the command node stuff.js, but in this case, I'd like to run a script called stuff.js with the arguments "blah", "hee", "woohoo!".

1
36
12/13/2016 6:46:11 PM

Accepted Answer

See http://nodejs.org/docs/latest/api/process.html#process_process_argv

In summary you'll run it like

node stuff.js blah hee "whoohoo!"

Then your arguments are available in process.argv

48
10/17/2012 1:27:44 AM

If you want to do more sophisticated stuff, the following modules are really helpful:

And for fun


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon