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).

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:
-
Directly in the terminal command line
- Unix/Linux:
1export PORT=3000
- Windows
1set PORT=3000
- Unix/Linux:
-
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:
|
1 |
npm install dotenv |
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:
-
Calling the config() method from code:
123456import dotenv from 'dotenv';dotenv.config();// ilirequire('dotenv').config(); -
Using a script within the package.json file:
1"start": "ts-node src/app.ts --env-file=.env"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:
|
1 2 3 4 |
// access environment variables via process.env const port = process.env.PORT; const dbUrl = process.env.DATABASE_URL; const host = process.env.HOSTNAME; |
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.
- Directly in the code when calling the config() method, when it is passed a path:
123456import dotenv from 'dotenv';dotenv.config({ path: '.env.development' })// ili sve u jednom redu:require('dotenv').config({ path: '.env.development' }); -
Define the corresponding file in the scripts in the package.json file:
or e.g.1"start": "ts-node src/app.ts --env-file=.env.production"
1"dev": "nodemon --watch src --exec ts-node src/app.ts --env-file=.env.development"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!!! -
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:
1NODE_ENV=development - Defining the NODE_ENV variable in the terminal before running the application:
1NODE_ENV=production ts-node app.ts -
Defining the NODE_ENV variable within the script in the package.json file:
or e.g.1"start": "NODE_ENV=production ts-node app.ts"
1"dev": "nodemon --watch src --exec ts-node src/app.ts NODE_ENV=development"
After which we can use as follows:
12const env = process.env.NODE_ENV || 'development';require('dotenv').config({ path: `.env.${env}` }); - Defining the NODE_ENV variable in the .env file itself:
