Environment variables = ENV variable

Environment variables = ENV variable

What are environment variables

Environment Variables (Environment Variables) are used in various places in software development and IT infrastructure, such as code: operating systems (“PATH” variable that defines the directories in which the operating system searches for executable files), software development (variables for storing configuration parameters such as API keys, databases, URLs..), security (for storing confidential information instead of hardcoding it into the application code).

env

Environment variables in Node

Environment variables are key components for working with Node.js applications, they allow configuration of the application without the need to change the code. A particularly useful tool when working with different environments (development, testing or production).

Setting variable values

You can set environment variables in two ways:

  1. Directly in the terminal command line
    • Unix/Linux:
    • Windows
  2. Inside special files (.env )

    By using one file we group all the variables and thus enable better organization.

Availability of variables

In order for the variable to be available somewhere in the application code, our code needs to have access to the .env file. Access to the variables from the .env file is achieved through the “process” object. It is a global object that provides information and control over the current working process of a Node.js application. It is part of the standard Node.js library and is available in all modules without the need to include additional packages. To load (environment) variables from the .env file into process.env, we need to install the dotenv package:

Loading .env contents into process.env object

After installing the dotenv package, we can call the dotenv.config() method which will load the contents of the .env file into a “process.env”object. Calling the config() method can be done in two ways:

  1. Calling the config() method from code:

  2. Using a script within the package.json file:

    NOTE:
    If the env-file is set within the script in the package.json file, then it is not necessary to call the config() method in the code itself!!!

Only after that can these static variables be used anywhere in the code as follows:

NOTE:
The .env file should be added to the .gitignore file to avoid sharing it with version controls, thus protecting sensitive information. Please note that in production, don’t forget to “manually” create a new .env file because it is not shipped with git !!!

Path to variable (“path”)

Sometimes it is useful to have different .env files for different environments, for example, .env.development, .env.test, .env.production. You can load the appropriate file based on the current environment in several ways:

It is also good to define the environment type at the application level, and we can do this in several ways.

  1. Directly in the code when calling the config() method, when it is passed a path:
  2. Define the corresponding file in the scripts in the package.json file:
    or e.g.

    NOTE:
    If the env-file is set within the script in the package.json file, then it is not necessary to call the config() method in the code itself!!!
  3. Define the NODE_ENV variable, then onbased on it, get the path with path: .env.${env} :

    • Defining the NODE_ENV variable in the .env file itself:
    • Defining the NODE_ENV variable in the terminal before running the application:
    • Defining the NODE_ENV variable within the script in the package.json file:
      or e.g.

    After which we can use as follows: