Tolga Açış
Tolga Açış

Reputation: 168

Setting up ConfigModule for .env doesn't work despite doing it as documentation shows

I am following instructions of documentation at https://docs.nestjs.com, specifically https://docs.nestjs.com/techniques/configuration to set up ConfigModule

This is how my files look like:

enter image description here

This is how my app.module.ts, where ConfigModule is set up as documentation instructs, looks like:

enter image description here

This is the contents of my .development.env file:

enter image description here

And this is the contents of my configuration.ts file:

enter image description here

To test, I set up my app.controller.ts as follows:

enter image description here

So here is the problem, like I said I'm setting up ConfigModule as documentation instructs, it does load configure.ts since I get the default value of 3000 for port but it doesn't get SERVER_PORT value of 8000 from .env file, so my problem is with ConfigModule not being able to find .env file. I am new to using .env files and NestJS, currently following documentation and I don't know if I did something documentation instructs wrong or if there is something documentation doesn't specify that I must do. I searched online and can't find a similar problem or example usages of ConfigModule that I can look at to see what I'm missing.

I noticed there is no .env in dist when NestJS is built so I'm assuming problem is with no .env being built into dist, but as I said I am new to .env and NestJS, I also looked up this problem too, and can't figure out what I must do for .env files to be built.

Upvotes: 0

Views: 272

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70111

.env files should be in the root of your project (same level a package.json) instead of with the source code (your src directory). If you move it to the root of the project it should work out just fine.

Upvotes: 3

Related Questions