24. Node.js Lessons.Reading Parameters From the Command Line and Environment.

Never miss a story from Soshace
Looks good!
Please enter correct name
Please enter correct email
Looks good!
Please confirm to subscribe!


Hey all! The first topic of this article is transferring of parameters and the script for Node.js. To show you the principle, we will create a file with a simple code, so add it (server.js):

and will order it to take a port for launching from a command line to launch it in the following way:

node server.js port=3000

and this port will be used for listen.

To do so, we can add this parameter to the launch configuration in your IDE (add port=3000). In order to get what has been transferred in a command line, you can perfectly use a variable called process.argv.

Launch it. Now you see the outputted parameters. Note that the first place belongs to node itself followed by the file and further by the port. Here you can easily see that the simplest ways to get the parameter value is to look through an array and get out the respective string. There is a more convenient way – to install a module called optimist:

npm install optimist

Use optimist instead of process.argvand we will see the results. To do so, I need to change the syntax because optimist supports either this kind of syntax:

// node server.js —port=3000


// node server.jsport 3000

Now I get the options through argvLaunch. As we can see, everything’s perfect. The only thing left to do is to use a variable and change the port within listen:

Restart and see how our server is working with the new port.


It’s perfect!

So, our next step now. Here node is used for launching, but we usually use another module for development – for example, supervisor, to enable it to restart the server automatically. Respectively, to use supervisor and transfer parameters, you need to do launch it as follows:

supervisor -- server.js --port=3000

Note: double hyphen is obligatory. The system won’t work without it. So, let us do respective changes within configurations. The supervisor module must be global. Launch it. Supervisor has reported to us what it’s been doing.

So, we can get the parameters of the command line, first of all, from the array process.argvsecond, from module optimistand third, you can get the settings from the environment variables. In general, most of them are set by the operating system – in particular, the HOME-named environment variable can be found almost anywhere. We can get it as follows:

So, launch it. And here it outputted a home directory of the current user.

You can use your own environment variables. For example, within the Express framework that we will study a little bit later, a  environment variable named  NODE_ENV–  is used. It stores information on what mode the launch has got. For example, NODE_ENV=development is a development mode or production is a mode of a live server. In the code, we can check: whether it is NODE_ENV = productionwe can apply extra optimization, otherwise, if it is NODE_ENV = development, we can connect additional debugging output.

In order to set the  environment variables we will use one of the several ways. The first one is: if the launch is via IDE, you can set up the environment variables  right there. Whether the launch is outside IDE, indication of  environment variables is executed differently, depending of the operating system. For example, it will be the following for Windows:

set NODE_ENV=production

All further commands launched through the terminal will have the value NODE_ENV=productionUnder Unix systems with Bash wrapping the analogue will be the following:

export NODE_ENV=production

Every further launch will inherit the variable. If you want to execute only one launch with this exact value, your  environment variable is equal to the value and a line further:

NODE_ENV=production supervisor -- server.js --port=3000

So, we’ve analyzed three ways to get process parameters in Node.js:

  1. Out of the command line through process.argv.
  2. Out of the command line, but via optimist module that can handle some standard setting types.
  3. Getting parameters out of  environment variables contained in process.env.

The lesson code can be downloaded from here.


The materials for this article were borrowed from the following screencast

We are looking forward to meeting you on our website soshace.com

Never miss a story from Soshace
Looks good!
Please enter correct name
Please enter correct email
Looks good!
Please confirm to subscribe!
Pavel Bragin 21 11 2016 Category:

Related articles


5 Awesome JavaScript Conferences to Attend in 2019

With web development being an incredibly popular domain, there’s no shortage of relevant events, meetups, and cons for web developers to gather.>>>


Yarn Package Manager in 2019: Should We Keep on Comparing Yarn with Npm?

Yarn was created by Facebook, Exponent, Google, and Tilde to improve upon the package management workflow. In this article, we’ll explore Yarn>>>


10 Sorting Algorithms Interview Questions: Theory and Practice for 2019

Proficiency in algorithms is such an essential part of programming knowledge that it’s hard to imagine great remote developers without it. The>>>

Human Resources