DEV Community

Cover image for How to manage environment variables on NestJS
Rubin
Rubin

Posted on

How to manage environment variables on NestJS

Environment variables allow us to manage the configuration of our applications separate from our codebase. Separating configurations make it easier for our application to be deployed in different environments(development,test,production).Also,large applications tend to have many environment variables.

If you are working on some project with nestjs and want to configure your app secrets and credentials in a more efficient way, you came to the right place.In this article, I would like to share initial configuration and some tip to use environment variables with Nestjs.

Description

A common application level tool is nestjs-easyconfig which allows us to load environment variables from a file named .env[producton|dev|test].
Nestjs-easyconfig loads configs from your .env (Wraps dotenv module) ⚙️ 🔥

Installation

$ npm install nestjs-easyconfig
$ yarn add nestjs-easyconfig
Enter fullscreen mode Exit fullscreen mode

Usage

With config file supplied (basic):

import  { Module }  from  '@nestjs/common';
import { EasyconfigModule } from  'nestjs-easyconfig';

@Module({
 imports:  [EasyconfigModule.register({path: './config/.env'})],
})
export  class  AppModule  {}
Enter fullscreen mode Exit fullscreen mode

With config file supplied and safe set to true:

import  { Module }  from  '@nestjs/common';
import { EasyconfigModule } from  'nestjs-easyconfig';

@Module({
 imports:  [EasyconfigModule.register({path: './config/.env', safe: true})],
})
export  class  AppModule  {}
Enter fullscreen mode Exit fullscreen mode

By default safe is set to false. When safe is set to true, the module compares the supplied env
file with the sample env file to find missing keys. If any keys which are in .env.sample but not in the evironment used, it is immediately reported in console.

Note: To use this, a sample env file .env.sample should be placed in the root dir

Without config file supplied:

import  { Module }  from  '@nestjs/common';
import { EasyconfigModule } from  'nestjs-easyconfig';

@Module({
 imports:  [EasyconfigModule.register({})],
})
export  class  AppModule  {}
Enter fullscreen mode Exit fullscreen mode

In this case, you have to pass in the NODE_ENV value and the .env file to read will be determined accordingly.
Loads environment variables from .env.[development|test|production][.local] files
For example, NODE_ENV=dev will make the app read .env.dev

Note: The .env file also has to be in root folder

Stay in touch

License

The package is MIT licensed.

Support on Beerpay

Hey dude! Help me out for a couple of 🍻!

Beerpay Beerpay

Discussion (4)

Collapse
pitops profile image
Petros Kyriakou

Why use this instead of nestjs/config package?

Collapse
rubiin profile image
Rubin Author

Well, this was before nestjs/config was a thing. Also it provides some features like env safety which nestjs/config doesnt at the moment

Collapse
pitops profile image
Petros Kyriakou

Actually in nestjs documentation, it is documented that you can use joi to provide env safety

Collapse
mouhsineelachbi profile image
MOUHSINE ELACHBI

Or you can only use .env file with a ts file full of your environment configurations. means no need for more packages or complications.