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.

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

Step 1: Installing Dependencies

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

Install the NestJS config package by running:

npm install @nestjs/config

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

Step 2: Creating ENV Files

Storing environment variables in a file lets you access them easily from any language, across different OSes. You can version control these .env files, 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.

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

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.

Step 3: Setting Up the Config Module

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

  1. In your project’s root module (app.modue.ts) file, import ConfigModule from @nestjs/config.
  2. Add ConfigModule to your imports array and call the forRoot method on it.
  3. Pass a configuration object to the forRoot method, with an isGlobal property to true. This option shares the configuration through the other modules in your application, meaning that you won’t have to set it up more than once.
  4. Specify your envFilePath in your configuration object. This property can either be a string (if you have one .env file) or an array containing all your .env files and will tell the config module which files to look for.

imports: [
isGlobal: true,
envFilePath: 'Name(s) of your .env file(s)',

Step 4: Using the Config Service to Read Environment Variables

To access the configuration values start by importing ConfigService from @nestjs/config. Inject it into the class’s constructor by declaring a private variable and assigning ConfigService as its type.

For example:

constructor(private config: ConfigService) {}

To access a variable, call the get method on the ConfigService on your private variable. Pass it the data type you require as a generic, and the name of the environment variable you want to access.

For example:

const envVar = this.config.get<string>('ENV_VALUE');

The ConfigService looks for a value with the name “ENV_VALUE” and returns its value.

Note that if two .env files contain the same property name, the first one specified in the envFilePath will 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 can 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.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button