Command-line arguments (CLI) are strings of text used to pass additional information to a program when an application is running through the command line interface of an operating system. We can easily read these arguments by the global object in node i.e. process object.
Below is the approach with the proper example.
Example 1:
- Step 1: Save a file as index.js and paste the below code inside the file.
vararguments = process.argv ;console.log(arguments) ;chevron_rightfilter_none - Step 2: Run index.js file using below command:
node index.js
- Output:

The process.argv contains an array where the 0th index contains the node executable path, 1st index contains the path to your current file and then the rest index contains the passed arguments.
Example 2: Program to add two numbers passed as arguments.
- Step 1: Save the file as index1.js and paste the below code inside the file.
vararguments = process.argvfunctionadd(a, b) {// To extract number from stringreturnparseInt(a)+parseInt(b)}varsum = add(arguments[2], arguments[3])console.log("Addition of 2, 3 is ", sum)chevron_rightfilter_none - Step 2: Run index1.js file using below command:
node index1.js
- Output:

So this is how we can handle arguments in Node.js. The args module is very popular for handling command-line arguments. It provides various features like adding our own command to work and so on.
Recommended Posts:
- Command Line Arguments in ElectronJS
- Nodejs | Automatic restart NodeJs server with nodemon
- How to read a file line by line using node.js ?
- How to execute PHP code using command line ?
- How to execute TypeScript file using command line?
- How to create optional arguments for a SASS mixin?
- How to describe “object” arguments in jsdoc?
- How to convert arguments object into an array in JavaScript ?
- Difference between multiple arguments and options object
- How to create optional arguments in PHP ?
- JavaScript TypeError - More arguments needed
- JavaScript ReferenceError Deprecated caller or arguments usage
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Web-Socket in NodeJS
- Signup Form Using Nodejs and MongoDB
- Dockerizing a simple Nodejs app
- Nodejs | DNS
- Encrypting Data in NodeJS
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : bunnyram19

