Node.js is an event-based, open-source, and asynchronous I/O framework that uses Google’s V8 JavaScript engine. We use it to develop applications that use JavaScript both on the server and client sides. Node.js applications are written in JavaScript.

Node.js applications also accept command-line arguments like any other programming language. By default, Node.js is able to handle your arguments but if you want some extra features then you can use third-party tools and packages like yargs and minimist. In this article, we will see how you can parse command line arguments in Node.js using process.argv, yargs, and minimist.

What are Command Line Arguments?

A command-line argument is a string of additional information or commands that are passed into a program while running it through the Command Line Interface(CLI) of an OS. These arguments usually include information used to set property or configuration values for an application.

In Node.js, all the command-line arguments given to the shell are forwarded to the process in an array called ‘argument values(argv).’ Later Node.js uses this array for every running process in the form of process.argv or you can say arguments are by default stored in the process.argv.

Why Use Command Line Arguments?

Command-line arguments are very flexible as they are passed as strings to your program and string data types can easily be converted to other types.

It allows you to pass information to an application before it starts and also you can pass an unlimited number of arguments through Command-Line.

What’s the example syntax of Command-Line Arguments?

Here is an example syntax, how the command line arguments look like:

 [runtime] [script_name] [argument-1 argument-2 argument-3 ... argument-n]

Arguments are separated by space and runtime could be anything like sh, java, node, etc.

How to Parse Command Line Arguments in Node.js

Here we will first see to parse arguments using process.argv and later with yargs and minimist.

1. Parse Arguments using process.argv

Using process.argv is the simplest way of parsing arguments in Node.js and you do not need to install any additional package or library for that. You just pass arguments to a Node.js application and these arguments are handled by process.argv by default.

There are three primary elements of process.argv array:

  • First, file system path pointing to the node executable.
  • Second the name of the JavaScript file that is being executed.
  • Third, the first argument that is passed by the user.

Example – We will write a simple Node script that will print all of the command line arguments passed to the application.

  • Create a file named ‘processargv.js’ and paste the following code.

    ‘use strict’;

    for (let j = 0; j < process.argv.length; j ) {

        console.log(j ‘ -> ‘ (process.argv[j]));

    }

  • Now run ‘processargv.js’ using the following command by passing argument.
    node processargv.js I am Node 
    

    Here we are passing three arguments to the file and as a result, you will see that ‘I’, ‘am’, ‘Node’ is stored at the 2nd, 3rd, and 4th indexes respectively. And the output will look like this:

    How to Parse Command Line Arguments in Node.js node node.js nodejs

2. Passing Arguments Using Yargs

Yargs is a Node.js module that can help you in passing arguments. It allows you to pass arguments in the form of key-value pairs and you can even access the argument values later in the program using the keys.

  • Install yargs module using this command.
    npm i yargs 
    
  • Now save the below code in a file named ‘yargs.js’ and while executing the program we will provide values for name and age.

    ‘use strict’;

    const args = require(‘yargs’).argv;

    console.log(‘Name: ‘ args.name);

    console.log(‘Age: ‘ args.age);

  • For executing the following command, execute the following code in your prompt. We are passing the values for name and age.
    node yargs.js --name=node --age=15 
    

    The output will look like this:

    How to Parse Command Line Arguments in Node.js node node.js nodejs

To understand more advanced features of yargs visit this advanced guide.

3. Parsing arguments using Minimist module

Another way of parsing command-line arguments to the Node.js applications is by using the minimist module. It parses arguments from the process.argv array and then transfers it into an easy-to-use associative array. This associative array can be used to access the elements using index names.

  • Install the minimist module by using this command.
    npm i minimist 
    
  • Now save the following script named ‘minimist.js’. Here we are using the slice method which will remove all prior array elements starting from the index passed to it as the parameter.

    ‘use strict’;

    const args = require(‘minimist’)(process.argv.slice(2));

    console.log(args.i);

    console.log(args.n);

  • While passing the argument we can also specify the character by which we want to access the elements. Here we will specify ‘i’ as the name for the second index and then ‘n’ as the name for the third index. Like this:
    node minimist.js –i node –n 15 
    

    The output will look like this:

    How to Parse Command Line Arguments in Node.js node node.js nodejs

Though minimist has not as many rich features as yargs still you can use it to make your work easy. For more detailed information about minimist visit here.

Conclusion

Argument parsing is a simple and important topic if you work on Node.js. There are multiple options available to make your work easy. So choose according to your needs if you want rich and advanced features then go with yargs otherwise choose from the process.argv and minimist.