node run parameters
// Sample Node.js script to explain run parameters
// Importing the 'process' module from Node.js
const process = require('process');
// Accessing command line arguments using process.argv
const args = process.argv;
// Printing the command line arguments
console.log('List of command line arguments:');
args.forEach((val, index) => {
console.log(`${index}: ${val}`);
});
Explanation:
- Import the 'process' module from Node.js:
const process = require('process');
The 'process' module provides information about the current Node.js process.
Access command line arguments using
process.argv
.process.argv
is an array that contains the command line arguments.The first two elements of
process.argv
are:process.argv[0]
: The path to the Node.js executable.process.argv[1]
: The path to the executed script.
Store command line arguments in the
args
array:const args = process.argv;
Iterate through the
args
array and print each command line argument usingconsole.log()
:javascript args.forEach((val, index) => { console.log(`${index}: ${val}`); });
- This loop prints the index and value of each command line argument in the format
index: value
.