How to pass command line arguments to Node.js




Generally developers who are coming from client side JavaScript development world don’t have to deal with the command line arguments.
But when developing Node.js application often developers need the Linux/Unix command line arguments.

Node.js provides an easy way to access command line argument in your code.
The arguments given from command line are stored in process.argv.
process.argv is an array containing the list of command line arguments.

  • The first element of process.argv is ‘node’.
  • The second element is the JavaScript file name.
  • Next element is any command line argument you will give.

Lets say we have a file called app.js. write the following code in app.js

now run you app.js code using following command and see the output.

 

Node.js Certification Training at Simplilearn.com!


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com