NestJS multi .env using 'nestjs-easyconfig'.
Tags: typescript, javascript, node, nestjs
Published: 2021-01-11 07:14:48

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'](https://www.npmjs.com/package/nestjs-easyconfig) with NestJS framework. You can find out full documentation about Nestjs [here](https://nestjs.com).

### 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](https://dev-to-uploads.s3.amazonaws.com/i/n2x6pc4seltnqmbpq8m6.png)

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
```
```
DB_HOST=localhost
DB_USERNAME=postgres
DB_PASSWORD=postgres
DB_NAME=database_name
DB_PORT=5432
PORT=3001
NODE_ENV=production
```

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

```
NODE_ENV=development
```
*or*
```
NODE_ENV=production
```

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

```javascript
//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');
    }
}
```

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

```javascript
//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 {}
```

Simply change your `.env` in root folder to select which environment to use.
[Source Code](https://github.com/danudenny/nestjs_multienv).