#ConfigModule caching old .env?

13 messages · Page 1 of 1 (latest)

zinc vigil
#

Hey all, I'm using the config module and trying to load .env variables into my app. This was working before but I just upgraded to nestjs 11 I believe and now whenever I make a change to the .env file NestJS doesn't use the new value. It uses the old value. Has anyone else experienced this?

next hatch
#

it does not seems to be related with nestjs

#

if you stop the process and start it again, it still uses the wrong value? 🤔

zinc vigil
#

Yes

#

And this is on my own macbook, not using Docker and I don't have any shell overrides

#

I tried doing this in main.ts import * as fs from 'fs'; console.log('ENV FILE CONTENTS:', fs.readFileSync('.env.test', 'utf8')); and the file does exist and it shows the right output but then I console log this

console.log('DATABASE_URL:', process.env.DATABASE_URL); and it shows the old value from the .env for some reason

next hatch
#

well, then it's not related with nestjs but with your setup

#

are you trying to read .env or another file (.env.test)?

zinc vigil
#

I'm reading from .env.test which is in the root of the project

#

and my config module is configured accordingly

ConfigModule.forRoot({
  envFilePath: ['.env.test'],

}),
#

And this started happening after I upgraded NestJS it's been fine for the past 8 or so months otherwise

zinc vigil
#

Fixed by adding this at the top

import * as dotenv from 'dotenv';
dotenv.config({ path: '.env.test' , override: true}); // or just dotenv.config() for .env

I think this is a Cursor issue, somewhere its caching the .env variables and loading them hopefully there is a better fix somewhere out there

next hatch
#

I'd say that something else is reading .env for you