DEV Community

Cover image for NestJS multi .env using 'nestjs-easyconfig'.
Denny Danuwijaya
Denny Danuwijaya

Posted on

NestJS multi .env using 'nestjs-easyconfig'.

Environment variables commonly used in many programming language as a variable which used to save such information like database credentials, caching credentials, and many more. With environment variables, we can call a variables easily.

In this tutorial, i will explain how to make multiple environment variables and consume it to another service / controller using 'nestjs-easyconfig' with NestJS framework. You can find out full documentation about Nestjs here.

Installation

I assumed you have a nestjs project, or you can make fresh project using nestjs cli nest new <project_name>. If all ready to setup, we can install nestjs-easyconfig using npm i nestjs-easyconfig.

Next step is create environment directory in root directory. and create two different .env files .env.production and .env.development. In this case, we can separate 2 different environment, which is the credentials is totally different each others. If you only have one environment, don't use this method! Simply use one .env and put it in root directory.

Alt Text

Write your credentials in all .env files on environtment folder. For example, i will write my credentials in .env.development and .env.production

DB_HOST=localhost
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_NAME=database_name
DB_PORT=5432
PORT=3002
NODE_ENV=development
Enter fullscreen mode Exit fullscreen mode
DB_HOST=localhost
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_NAME=database_name
DB_PORT=5432
PORT=3001
NODE_ENV=production
Enter fullscreen mode Exit fullscreen mode

You can choose which environment to use using .env files in root directory.

NODE_ENV=development
Enter fullscreen mode Exit fullscreen mode

or

NODE_ENV=production
Enter fullscreen mode Exit fullscreen mode

Configuration

Create config folder in src directory. Then create service file easyconfig.service.ts.

//easyconfig.service.ts

import { Injectable, OnModuleInit } from "@nestjs/common";
import { EasyconfigService } from "nestjs-easyconfig";
import * as dotenv from 'dotenv';
import * as fs from 'fs';

@Injectable()
export class EasyConfiguration implements OnModuleInit {
    constructor(private easyConfigService: EasyconfigService) {}

    onModuleInit() {
        return this.easyConfigService.get('envConfig');
    }
}
Enter fullscreen mode Exit fullscreen mode

Add easyconfig module in app.module.ts and call easyconfig service in app.module.ts providers.

//app.module.ts

import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { EasyconfigModule } from 'nestjs-easyconfig';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { EasyConfiguration } from './configs/easyconfig.service';
require('dotenv').config();

@Module({
  imports:  [EasyconfigModule.register({path: `environment/.env.${process.env.NODE_ENV}`, safe: true})],
  controllers: [AppController],
  providers: [AppService, EasyConfiguration],
})
export class AppModule {}
Enter fullscreen mode Exit fullscreen mode

Simply change your .env in root folder to select which environment to use.
Source Code.

Top comments (1)

Collapse
 
kevinmerckx_47 profile image
Kevin Merckx

Hello! Nice article, You should consider using nx.dev/ as an alternative. You can configure multiple environments with it: dev, prod, e2e, etc.