How do I pass command line arguments to a Node.js program?
How do I pass command line arguments to a Node.js program?
449
29-Apr-2023
Updated on 01-May-2023
Aryan Kumar
01-May-2023In Node.js, command line arguments can be passed to a program using the process.argv array. This array contains the command line arguments passed to the Node.js process, where process.argv[0] is the path to the Node.js executable, process.argv[1] is the path to the script being executed, and the remaining elements of the array are the command line arguments.
Here is an example of how to access command line arguments in a Node.js program:
In this example, we use the process.argv array to get the command line arguments passed to the program. We use the slice() method to extract the elements of the array starting from index 2, which skips the first two elements that correspond to the Node.js executable and the script being executed. The remaining elements of the array are the command line arguments.