How to pass command line arguments to a Node.js program?

Sometimes, we want to pass command line arguments to a Node.js program.

In this article, we’ll look at how to pass command line arguments to a Node.js program.

How to pass command line arguments to a Node.js program?

To pass command line arguments to a Node.js program, we can get them from the process.argv array property.

For instance, we write

process.argv.forEach((val, index, array) => {
  console.log(index, val);
});

to call process.argv.forEach with a callback to get the value for each command line argument in our Node app.

val has the command line argument value at the given array index.

Conclusion

To pass command line arguments to a Node.js program, we can get them from the process.argv array property.