Thanks for visiting! - Alex

Nodejs: Accept arguments from cli

Estimated reading time: 1 min

How to make this work?

node code.js <args>

You don’t need to import anything. Open a new file and start coding.

Node’s core provides a process object that exposes process.argv property. This has what we need. This property exposes an array. First two items in the array are :

  • process.argv[0] - location of node and
  • process.argv[1] - location of the script being executed, respectively.

The entries after, process.argv[2...process.argv.length], will be used for user input arguments.

You can get to them like this :

// code.js
const args = process.argv.slice(2); => {

Then re-run the program again and terminal will print out the arguments one at a time; like this :

$ node code.js user:alice password:onetwothreefourfice
> user:alice
> password:onetwothreefourfice

Problem solved 👍


· nodejs, tips
👈 Walking Away
Task Management Strategy 👉

⭐️ Learn Node.js ⭐️