Passing command line arguments to Node.js program

You want to access arguments in javaScript for passing command line arguments to Node.js program. The arguments of Node.js are stored in process.argv and it returns an array which contains the command line arguments that needs to be passed to Node.js when it is launched. The first element that is passed is ‘node’, second will be the name of the javaScript file. Remaining elements will be all other additional command line arguments. Refer node docs to know more about arguments.

process.argv.forEach(function (val, index, array) {

  console.log(index + ': ' + val);

});


This will produce an output as:

0: /usr/local/bin/node

1: /Users/mjr/work/node/process-args.js

2: one

3: two=three

4: four