argv

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.

// print process.argv
process.argv.forEach(function (val, index, array) {
  console.log(index + ': ' + val);
});

This will generate:

$ node process-2.js one two=three four
0: node
1: /Users/mjr/work/node/process-2.js
2: one
3: two=three
4: four

It’s important to note that you will need to use quotes if you want an argument with spaces:

$ node process-2.js one two=three four "argument with spaces"
0: node
1: /Users/mjr/work/node/process-2.js
2: one
3: two=three
4: four
5: argument with spaces

Also arguments that contain variable expansions will require single quotes or escaping to prevent expansion:

$ node process-2.js one two=three four '$Will this be expanded? I wonder!'
0: node
1: /Users/mjr/work/node/process-2.js
2: one
3: two=three
4: four
5: $Will this be expanded? I wonder!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s