Skip to content

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

See also  SyntaxError: Cannot use import statement outside a module - How to fix this error in Node.js?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.