In Node.js the command-line arguments are captured in the object process.argv. Let’s start by logging them to the screen.

console.log(process.argv);

If you run your script without any arguments, the output will be something like this.

[ '/usr/local/bin/node', 'index.js' ]
  • '/usr/local/bin/node' is the path to the node.js interpreter
  • 'index.js' is the name of the js file that you’re running

When you add custom arguments to your script, they will appear in the process.argv array as well.

Let’s try to add the command line arguments hello and 125.

> node index.js hello 125

The output changes accordingly.

[ '/usr/local/bin/node', 'index.js', 'hello', 125 ]