Node.js Command Line Arguments

To access Command Line Arguments in Node.js script file, use process.argv array that contains command line arguments that were passed when starting Node.js process.

Command line arguments are usually used when your program is so generalized and you need to send in some values for the program to work on. A simple example would be a summer which calculates sum of two numbers. You need to provide the two numbers as arguments. Another example would be to load a configuration file. When you start Node.js process, you provide this configuration file to start the application in one of many modes your use cases demand.

Example

In this Node.js Tutorial, we shall learn how to access Node.js command line arguments with the help of an example.

By default argument 0 is the path to node program and argument 1 is the path to the Node Java Script file. The rest are the additional arguments provided to the Node.js. White space characters are considered as separators for the arguments.

Conclusion :

In this Node.js Tutorial, we have learnt how to provide and access Command Line Arguments in Node.js script file.