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.Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3