Follow @endyourif rss Facebook LinkedIn

Passing/reading command line arguments with Node.js Passing/reading command line arguments with Node.js

Published on Jan 21, 2020

It's quite common when running an application to want to pass arguments to the program to define how it should run when executed. This is no different then with Node.js. To accomplish this I like to use one of two ways:

  1. Reading the JavaScript array process.argv and manually parsing the variables

  2. Using an npm library such as minimist that helps simplify and (in my opinion) parses them into a nice JavaScript variable

Let's take a look at both ways.

Reading Command Line Arguments with process.argv

process.argv is an array that can be accessed in your Node.js script. Because it is an array you can a specific element in the array or loop the array and build your variables in the loop:

process.argv.forEach(function (value, index, array) {
console.log(index + '=' + value);

Then you can run your application as follows: node index.js arg1 arg2 arg3.

This will output:

It's really important to note that the first two elements in the array are the command node and the full path to the script being executed. Your arguments don't start until the 3rd element in the array.

This works well for very simple parameters, but it's nice to provide named elements with specific values. For this you can write something custom to parse them out or use an existing library. I'm a big fan of not reinventing the wheel, so let's look at a lightweight library that does it all for us.

Reading Command Line Arguments with minimist

Because this is an npm package you of course need to install the package in your Node.js project: npm install minimist. Now we can use this in our script as follows:

var parameterInput = require('minimist')(process.argv.slice(2));

The library still uses the process.argv array to read the arguments, but notice the use of .slice(2). This removes the first two arguments mentioned above of the command node and the full path to the script being executed.

In our previous example we ran the application as follows: node index.js arg1 arg2 arg3. Using the minimist library it would convert the names to a JSON variable with named elements, e.g.:

arg1: true,
arg2: true,
arg3: true

Because I didn't provide a value for this named arguments, the library interprets them as a boolean. With this library we can do much more powerful arguments:
node index.js -env local -port 8080 -https

Which would create a JSON object as follows:

env: 'local',
port: '8080',
https: true

This would allow us, as an example, to start an Express web server using a variable from the input instead of hard-coding in the script:

app.listen(parameterInput.port, function() {
console.log('Running on port: ' + parameterInput.port);

Much nicer in my opinion!

Tags: Node js Tutorial | slice | minimist

My Books
ASP.NET MVC 5 With Bootstrap and Knockout.js
Knockout.js Building Dynamic Client-Side Applications
20 Recipes for Programming MVC 3
20 Recipes for Programming PhoneGap
Rapid Application Development with CakePHP