Command-line arguments
JavaScript, Node · Apr 26, 2022

Gets the command-line arguments passed to a Node.js script.
- Use
process.argv
to get an array of all command-line arguments. - Use
Array.prototype.slice()
to remove the first two elements (path of the Node.js executable and the file being executed).
const getCmdArgs = () => process.argv.slice(2);
// node my-script.js --name=John --age=30
getCmdArgs(); // ['--name=John', '--age=30']
Written by Angelos Chalaris
I'm Angelos Chalaris, a JavaScript software engineer, based in Athens, Greece. The best snippets from my coding adventures are published here to help others learn to code.
If you want to keep in touch, follow me on GitHub.