Using the NestJS Config Module to Manage Environment Variables

An environment variable provides information about the environment a process is running in. They configure server ports and database connections, hide sensitive data like API keys, and much more.

The NestJS way of reading environment variables differs from NodeJS’s standard dotenv package.

4

The NestJS config module lets you manage your environment variables in just a few steps.

Step 1: Installing Dependencies

NestJS provides a dedicated@nestjs/configpackage that uses thedotenvpackage under the hood. This zero-dependency package loads environment variables from a.envfile intoprocess.env. The process.env object is a global variable injected at runtime for your application’s use.

Install the NestJS config package by running:

Person holding the Google Pixel 9a showing the back of the phone

The NestJS config package works by exposing a config module and a config service to the application. The config module specifies the.envfile the application to read. At the same time, the config service exposes the information inside the.envfile to the rest of the application.

Step 2: Creating ENV Files

Storing environment variables in a file lets youaccess them easily from any language, across different OSes. You can version control these.envfiles, so they increase project portability and can ease debugging woes.

The NestJS approach for creating .env files differs from the official dotenv recommendation. According to the dotenv documentation, you should not create more than one .env file in an application. NestJS lets you create multiple .env files with different names.

iCloud+ Website on MacBook Sitting on Kitchen Island

As good practice, you should always create .env files in your project’s root directory and include them in your.gitignorefile.

There’s no special way to create a .env file—just create and edit them with your normal text editor—but they must start with .env. For example,.env.development.

iPhone in hand showing keyboard and paste option in notes app

Step 3: Setting Up the Config Module

Follow the step below to set up your Config Module globally and specify the.envpaths:

Step 4: Using the Config Service to Read Environment Variables

To access the configuration values start by importingConfigServicefrom@nestjs/config. Inject it into theclass’s constructorby declaring aprivatevariable and assigningConfigServiceas its type.

For example:

Several lines of javascript code syntax-highlighted in different colors

To access a variable, call thegetmethod on theConfigServiceon yourprivatevariable. Pass it the data type you require as a generic, and the name of the environment variable you want to access.

TheConfigServicelooks for a value with the name “ENV_VALUE” and returns its value.

Note that if two.envfiles contain the same property name, the first one specified in theenvFilePathwill take precedence.

The Importance of Environment Variables

Environment variables are an essential part of a program, particularly in more complex applications. They let you control your program’s configuration through an easy-to-understand, common mechanism.

you could use environment variables to control all aspects of configuration. From different database settings to sensitive data like API keys and credentials, they let you change configuration without touching the underlying source code.

Set custom preferences and default applications on Linux by creating and configuring environment variables.

OneDrive is one of the best, but it has a catch.

The best features aren’t the ones being advertised.

Not all true crime is about hacking, slashing, and gore.

Sometimes the smallest cleaning habit makes the biggest mess.

Goodbye sending links via other apps.

Technology Explained

PC & Mobile