48

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!".

GG.
  • 21,083
  • 14
  • 84
  • 130
Anderson Green
  • 30,230
  • 67
  • 195
  • 328

5 Answers5

58

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

hexist
  • 5,151
  • 26
  • 33
  • 1
    Does this apply to Windows as well as Mac/Linux/Unix? – Anderson Green Oct 17 '12 at 01:31
  • 1
    Yep, that's standard on all platforms – hexist Oct 17 '12 at 01:34
  • 1
    Also, is the quote on the string "whoohoo" necessary (or is it a typo)? – Anderson Green Oct 17 '12 at 01:38
  • 4
    In *nix the exclamation mark is used for event designators in the shell, so it's good practice to quote or escape arguments where you want the literal character '!'. (Note that your program won't get the quotes as part of the string in the 5th entry of argv, it'll just be whoohoo!) – hexist Oct 17 '12 at 01:49
  • @hexist What if you had an argument called `ah hah` and needed to add that to the list of parameters? Would you also have to quote it like you did for `woohoo!` above so that the invocation is `node stuff.js blah hee "whoohoo!" "ah hah"`? The argv documentation doesn't get into details about vars with spaces, which is important because it appears to delimit on spaces. – Wimateeka Jul 30 '18 at 17:40
  • @Wimateeka Short answer: yes you need quotes if you have spaces. Long answer: That's not going to be documented in node because that's a os/shell thing, just as is the "whoohoo!" thing. Whatever invokes the node process is responsible for delimiting the arguments, not node. However the defacto standard used by shells and such is to require quotes when passing in arguments that have spaces or certain special characters, so yes, use quotes. – hexist Jul 30 '18 at 21:27
20

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

And for fun

dthree
  • 19,847
  • 14
  • 77
  • 106
zemirco
  • 16,171
  • 8
  • 62
  • 96
4

Nomnom is another possible solution.

thomaux
  • 19,133
  • 10
  • 76
  • 103
dylanized
  • 3,765
  • 6
  • 32
  • 44
2

This simple node module is also helpfull: command-line-args

It allows to define a simple definition:

const optionDefinitions = [
  { name: 'verbose', alias: 'v', type: Boolean },
  { name: 'src', type: String, multiple: true, defaultOption: true },
  { name: 'timeout', alias: 't', type: Number }  
]

It validates your options and allows you to access them in a simple way.

Andreas
  • 1,691
  • 1
  • 15
  • 34
1

The arguments are stored in

process.argv and to pass the arguments in command line please check below example:

ex. in this example below i have used commander NPM Module. var args = require('commander')

Options with commander are defined with the .option() method. The example below parses args and options from process.argv, leaving remaining args as the program.args array which were not consumed by options. here process.argv is An array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments after executing.

function list(val) {
    return val.split(',');
}
args.version('0.11.2')
    .option('-t, --tag [value]', 'tags to ignore', list, undefined)
    .parse(process.argv);

here to take input from command-line, we have to execute .js file with -t and after that arguments separated by comma(,)incase of multiple arguments ex. : node example.js -t tagname here i have used list to process multiple command line arguments ,so that we can pass multiple command line arguments ex. node example.js -t tagname1, tagname2 so after this , all input passed as command line arguments will be available in array named args, so can use this array for your purpose and you can read more about it from here:-

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

or you can make use of the following modules :

  1. commander:-

https://www.npmjs.com/package/commander

  1. yargs :-

https://www.npmjs.com/package/yargs

  1. vorpal :-

https://www.npmjs.com/package/vorpal

Rohit Jaiswal
  • 257
  • 4
  • 6
  • The question is about how to pass the arguments to the node script, not how to process them in the script. – amadour May 05 '19 at 20:33