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.argvyargs, 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.

#nodejs #node #node.js

How to Parse Command Line Arguments in Node.js
1.15 GEEK