How to read command line arguments in NodeJS?
Let’s assume you are passing a few values from the command line when you are running a node command.
$ node app.js 1 2 3
Advertisements
These values can be read inside your code using process.argv. All command-line arguments in NodeJS are stored in a global variable process.argv as an array.
console.log(process.argv); //['node','/home/app.js','1','2','3']
You can just read the values from this array using the indices.
let args = process.argv console.log(args[0]); //1 console.log(args[1]); //2 console.log(args[2]); //3