📝 Edit page
➕ Add page
Argument parsing
How to parse args given to a Node.js script
Resources
- Accept arguments from the command line on the Node.js Learn site
Code
Basic
Use process.argv. Drop the first two arguments, which are the path to Node and the current script.
const args = process.argv.slice(2)
console.log(args)
Example usage:
$ node app.js foo --bar=bazz
[ 'foo', '--bar=bazz' ]
Keyword arguments
You need a package like minimist to parse key-value pairs for you.
const minimist = require('minimist')
const args = minimist(process.argv.slice(2))
console.log(args['name'])
Example usage.
$ node app.js --bar=bazz
joe