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


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

About the author

Stay Informed

It's important to keep up
with industry - subscribe!

Stay Informed

Looks good!
Please enter the correct name.
Please enter the correct email.
Looks good!

Related articles


JAMstack Architecture with Next.js

The Jamstack architecture, a term coined by Mathias Biilmann, the co-founder of Netlify, encompasses a set of structural practices that rely on ...


Training DALL·E on Custom Datasets: A Practical Guide

The adaptability of DALL·E across diverse datasets is it’s key strength and that’s where DALL·E’s neural network design stands out for its ...


The Ultimate Guide to Pip

Developers may quickly and easily install Python packages from the Python Package Index (PyPI) and other package indexes by using Pip. Pip ...

No comments yet

Sign in

Forgot password?

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy

Password recovery

You can also try to

Or use a social network account


By Signing In \ Signing Up, you agree to our privacy policy