Home >Web Front-end >JS Tutorial >How Do I Access Command Line Arguments in Node.js?
Accessing Command Line Arguments in Node.js
When launching Node.js programs like node server.js folder, arguments can be passed as shown in the Node.js usage documentation: $ node -h.
How to Access Arguments in JavaScript
Node.js provides access to command line arguments through the process.argv array. The first element is always 'node', the second is the script file name, and the subsequent elements contain the arguments:
// Print the command line arguments process.argv.forEach(function (val, index, array) { console.log(index + ': ' + val); });
Example
Consider the following command: $ node process-2.js one two=three four.
The process.argv array for this command would be:
[ 'node', '/Users/mjr/work/node/process-2.js', 'one', 'two=three', 'four' ]
Note:
The standard method described above requires no additional libraries. However, there are also various command line parsing libraries available in Node.js, such as 'commander' or 'yargs', which can provide additional features and flexibility.
The above is the detailed content of How Do I Access Command Line Arguments in Node.js?. For more information, please follow other related articles on the PHP Chinese website!