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
minimist. In this article, we will see how you can parse command line arguments in Node.js using
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
1. Parse Arguments using process.argv
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.
- 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.
- 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:
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
- 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:
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
- 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:
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.
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