Dotenv from NPM & MONGO_URL not being set

Has anyone here gotten the npm version of dotenv working with the MONGO_URL env var?

I have dotenv working, but seems like there is something overwriting MONGO_URL back to the localhost path. Is there any way around this?

1 Like

Looks like https://github.com/okgrow/meteor-dotenv/issues/2
Meteor is overwriting it at some point on start up. I saw the cat workout which will probably work for us, but I’ll keep digging into how Meteor sets that var and when.

So what’s happening in case anyone is looking at the later.

MONGO_URL is set on dev build via the shell function. Dotenv does not support overwriting env vars.

Here’s what I did to fix. I made a env.ts file in my server folder that’s loaded first thing.

dotenv.config({
  path: `${process.env.PWD}/config/${process.env.ENV}.env`,
})

const envConfig = dotenv.parse(
  fs.readFileSync(`${process.env.PWD}/config/${process.env.ENV}.env`)
)

process.env.MONGO_URL = envConfig.MONGO_URL

This first loads dotenv, reads it, the overwrites MONGO_URL. I should note that the whole process.env.EVN thing is to have two separate env files prod.env and dev.env in a folder named config.

1 Like