read command line arguments in nodejs

How to Read Command Line Arguments in NodeJS

NodeJS is a powerful JavaScript framework that allows you to build web applications and websites using JavaScript. Sometimes, you may need to be able to pass command line arguments while running NodeJS processes. In this article, we will learn how to parse command line arguments in NodeJS.


How to Read Command Line Arguments in NodeJS

By default, the command line arguments used for invoking NodeJS server are stored in process.argv as an array. The first element in this array will be node, the second element will be the name of JavaScript file. The next elements will be additional command line arguments that you pass in your NodeJS command.

Here is a simple code snippet to display all command line arguments one by one, on each line. This will help you understand how the values are stored in process.argv.

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

Save the above snippet in your JavaScript file, say, process-2.js which you run using node command. Let us say you run your NodeJS file with the following command.

$ node process-2.js one two=three four

In this case, the above code snippet will display the following values.

0: node
1: /home/ubuntu/process-2.js
2: one
3: two=three
4: four

So you can easily refer to your command line arguments as process.argv[index] as shown below.

var program_name = process.argv[0]; //value will be "node"
var script_path = process.argv[1]; //value will be "yourscript.js"
var first_value = process.argv[2]; //value will be "first argument"
var second_value = process.argv[3]; //value will be "second argument"
...

Please note, sometimes the second element of your array may not be your JavaScript file’s filename. This is because NodeJS supports different syntaxes for invoking a process.

node [options] [ -e script | script.js ] [arguments] 
or 
node debug script.js [arguments]
or
node --harmony script.js [options]
or 
node --no-deprecation --enable-ssl2  script.js [options]

Depending on the which syntax you use, NodeJS will accordingly store the command’s constituents in process.argv array, and the above mentioned code snippet should help you understand how these values are stored.

In this article, we have seen how to pass command line arguments in NodeJS. This is a common problem for beginners who are starting out with NodeJS. We have only shown the plain JavaScript way of reading command line arguments. You can also use a third-party library for this purpose. Here are some popular NPM modules for command line parsing.

Minimist: For minimal argument parsing.

Commander.js: Most adopted module for argument parsing.

Meow: Lighter alternative to Commander.js

Yargs: More sophisticated argument parsing (heavy).

Vorpal.js: Mature / interactive command-line applications with argument parsing.

Also read:

How to Change Element’s Class in JavaScript
JavaScript Round to 2 Decimal Places
How to Disable Right Click in Web Page Using JavaScript
How to Disable/Enable Input in jQuery
How to Run 32-bit App on 64-bit Linux

Leave a Reply

Your email address will not be published. Required fields are marked *