AppDividend
Latest Code Tutorials

What is process.env in Node.js | Environment Variables in Node.js

0

In this tutorial, we will see What is process.env in Node.js | Environment Variables in Node.js. Working with environment variables is the great way to configure different configurations of your Node.js application. Many cloud hosts like Heroku, Azure, AWS, now.sh, etc. uses node environment variables. Node.js modules are using environment variables. Hosts, for example, will set the PORT variable that specifies on which port the server should listen to work properly. Modules might have the different behaviors (like logging) depending on the value of the NODE_ENV variable.

What is process.env in Node.js

The Node injects the process.env global variable at runtime in our app to use, and it represents the state of the system environment of our app when it starts. For example, if the system has the PATH variable set, this will be made accessible to you through the process.env.PATH variable which you can use to check where binaries are located and make external calls to them if required.

Why environment variable in Node.js is important

When we write the code, we can never be sure where our app can be deployed. If we require the database in development, we spin up the instance of it, and we link to it via a connection string , something like 127.0.0.1:3306. However, when we deploy it to the server in production, we might need to link it to the remote server, let say 32.22.2.1.

Now, we can write the code of linking the database using the process.env file.

const connection = new Connection(process.env.DB_CONNECTION_STRING);

Specifying an external service dependency allows us to link to the remote load-balancer protected database cluster which can scale independently of an app, and will enable us to have the multiple instances of our app independently of a database service.

Basics of process.env

Accessing environment variables in the Node.js is supported out of the box. When your Node.js process boots up, it will automatically provide an access to all the existing environment variables by creating the env object as a property of the process global object.

I am using Node.js version 11. Now, if you have not installed Node.js, then please install it.

After that, create a folder and inside create a file called app.js and add the following code.

console.log(process.env);

Now, go to the terminal and hit the following command.

What is process.env in Node.js

 

The above code should output all the environment variables that this Node.js process is aware of. If we want to access one specific variable, access it like any property of the object. Let’s access the PORT property.

console.log(process.env.PORT);

You will see the undefined in the output because we have defined a specific port yet.

Cloud hosts like Heroku or Azure; however, use the PORT variable to tell you on which port your server should listen for the routing to work correctly. Therefore, the next time you set up the web server, you should determine the port to listen on by checking PORT first and giving it a default value otherwise.

See the following code.

// app.js

const app = require('http').createServer((req, res) => res.send('Ahoy!'));
const PORT = process.env.PORT || 4000;

app.listen(PORT, () => {
  console.log(`Server is listening on port: ${PORT}`);
});

In the above code, we have created the HTTP server, and that server is running on the either defined port in the .env file or by default 4000.

Run the file and see the output.

Basics of process.env

 

Since the process.env is simply an ordinary object, we can set/override the values very easily.

Explicitly loading variables from the .env file

We have two levels to work, when dealing with a server provisioning: 1) infrastructure and 2) application levels. We can either set an environment through the application-level logic, or we can use the tool to provide an environment for us.

A standard application-level tool is dotenv, which allows us to load the environment variables from a file named .env. We can install it via NPM using the following command.

npm install dotenv --save

Afterward, add the following line to the very top of your entry file.

require('dotenv').config();

The above code will automatically load the .env file in the root of your project and initialize the values. It will skip any variables that already have been set.

You should not use the .env file in your production environment though instead set the values directly on the respective host. Therefore, you might want to wrap the config statement in the if-statement.

if (process.env.NODE_ENV !== 'production') {
  require('dotenv').config();
}

Now, create a file called .env and add the following one line of code inside that .env file. You need to create the .env file in the root of your project folder.

PORT = 3000

Now, write the following code inside the app.js file.

// app.js

if (process.env.NODE_ENV !== 'production') {
  require('dotenv').config();
}

const app = require('http').createServer((req, res) => res.send('Ahoy!'));
const PORT = process.env.PORT || 4000;

app.listen(PORT, () => {
  console.log(`Server is listening on port: ${PORT}`);
});

Now, run the server and you will see the server is running on PORT: 3000 and not on 4000.

While this is a convenient for development needs, it is considered the bad practice to couple the environment with our application, so keep it out by adding .env to your .gitignore file.

At an infrastructure level, we can use the deployment manager tools like PM2, Docker Compose, and Kubernetes to specify an environment.

PM2 uses an ecosystem.yaml file where you can specify the environment using the env property.

apps:
  - script: ./app.js
    name: 'my_application'
    env:
      NODE_ENV: development
    env_production:
      NODE_ENV: production
    ...

Docker Compose likewise allows for the environment property to be specified in the service manifest.

version: "3"
services:
  my_application:
    image: node:8.9.4-alpine
    environment:
      NODE_ENV: production
      ...
    ...

Kubernetes has an equivalent the env property in the pod template manifest which allows us to set the environment:

kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: my_application
spec:
  ...
  template:
    spec:
      env:
        - name: NODE_ENV
          value: production
        ...

Using the process.env.* correctly results in applications that can be tested with ease and deployed/scaled elegantly. So, we have seen process.env in Node.js very deeply in this article.

Finally, What is process.env in Node.js | Environment Variables in Node.js is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.