1

I am working on the NestJS along with TypeOrm (MySQL). Project itself is provisioned by Terraform, run by Jenkins and deployed on K8.

I will use process.env.******* for the DB Connection, and when it comes to the deployment (test, stage and prod), I really don't care. Jenkins provides the credentials (provided by Terraform).

However, I want to have a local mode, where it is friendly to other developers to start the service locally.

In my previous project, I had extra file in the root. That file was only wrapper, which loads dotenv and then the main app file.

Something like this:

require('dotenv').config();

const lambdaApp = require('./index');
lambdaApp.handler()

That was simple and easy to use. I just have .env.example file, and if you need if you set it up yourself.

I figured I shall do the same with the NestJS. Unfortunately, I am stuck.

If I were to use local.index.js to start the dotenv, then How can I load and execute the main.ts file. I could call the bootstrap() function, but it wont work.

Simple approach that did not work:

require('dotenv').config();

const mainApp = require('./main.ts');

mainApp.bootstrap();

The main.ts, needs to be converted to js from ts.

I could probably find some way to do it in code, but it's looks really wrong. There has to be a simpler way to achieve this, which, unfortunately I am not seeing.

Amiga500
  • 5,874
  • 10
  • 64
  • 117
  • https://stackoverflow.com/questions/63285055/nestjs-how-to-use-env-variables-in-main-app-module-file-for-database-connecti/63285574 - check this if it's helpful – critrange Aug 10 '20 at 11:11
  • Thank you yash. It was very helpfull. But I have found a way to do it by book. My scenario is already covered in the official documentation. It already uses dotenv under the hood. Simple. I will post an answer – Amiga500 Aug 10 '20 at 11:24

1 Answers1

1

This was the case of not reading the documentation and reinventing the wheel. In my defense, I can say there are so many things to read, I don't have time. That is pure truth, but time and reading can be managed. I should have checked the official documentation first, and I would find the answer in there.

Anyway, right here it is explained. I will not post any codes samples, since it is pointless to do it. They also use dotenv library and env file.

Amiga500
  • 5,874
  • 10
  • 64
  • 117