Node.js | process.argv Property

The process.argv property is an inbuilt application programming interface of the process module which is used to get the arguments passed to the node.js process when run in the command line.

Syntax:

process.argv

Return Value: This property returns an array containing the arguments passed to the process when run it in the command line. The first element is the process execution path and the second element is the path for the js file.

Below examples illustrate the use of process.argv property in Node.js:

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// process.argv Property
   
// Include process module
const process = require('process');
  
// Printing process.argv property value
console.log(process.argv);

chevron_right


Command to run the code:

node index.js extra_argument1 extra_argument2 3

Output:

[ 'C:\\Program Files\\nodejs\\node.exe',
  'C:\\nodejs\\g\\process\\argv_1.js',
  'extra_argument1',
  'extra_argument2',
  '3' 
]

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Node.js program to demonstrate the
// process.argv Property
   
// Include process module
const process = require('process');
  
// Printing process.argv property value
var args = process.argv;
  
console.log("number of arguments is "+args.length);
  
args.forEach((val, index) => {
    console.log(`${index}: ${val}`);
});

chevron_right


Command to run the code:

node index.js extra_argument1 extra_argument2 3

Output:

number of arguments is 5
0: C:\Program Files\nodejs\node.exe
1: C:\nodejs\g\process\argv_2.js
2: extra_argument1
3: extra_argument2
4: 3

Note: The above program will compile and run by using the node filename.js command followed by arguments.

Reference: https://nodejs.org/api/process.html#process_process_argv

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.