process.argv() Method in Node.js


The process.argv() method is used for returning all the command-line arguments that were passed when the Node.js process was being launched. The first element will always contains the same value as process.execPath.

Syntax

process.argv()

Parameters

Since it returns all the command line arguments passed before the node.js process. It does not need any inputs from the user.

Example

Create a file with name – argv.js and copy the below code snippet. After creating file, use the following command to run this code as shown in the example below −

node argv.js

argv.js

 Live Demo

// Node.js program to demonstrate the use of process.argv

// Importing the process module
const process = require('process');

// Printing property value for process.argv
console.log(process.argv);

Output

C:\home
ode>> node argv.js [ '/usr/bin/node', '/home/node/test/process.js' ]

Example

Let's take a look at one more example.

 Live Demo

// Node.js program to demonstrate the use of process.argv

// Importing the process module
const process = require('process');

// Printing process.argv property value
var args = process.argv;

console.log("Total number of arguments are: "+args.length);
args.forEach((val, index) => {
   console.log(`${index}: ${val}`);
});

Output

C:\home
ode>> node argv.js Total number of arguments are: 2 0: /usr/bin/node 1: /home/node/test/process.js

Updated on: 20-May-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements