#multiple env file

31 messages · Page 1 of 1 (latest)

strange halo
#

i have multiple environment files and importing them like this

ConfigModule.forRoot({
  isGlobal: true,
  validationSchema: Joi.object({
...
  }),
  envFilePath: [
    '.env',
    process.env.NODE_ENV !== 'production'
      ? `.env.${process.env.NODE_ENV}`
      : '',
  ],
})

My .env.test file have to use the variables from .env and replace it. i mean the results below.

After i seperate my env file. and console log DATABASE_URL the variable

mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}/${MONGO_DATABASE}

But when i put DATABASE_URL inside .env instead of .env.test variables inside of DATABASE_URL renders.

How can i solve this? Thanks

nimble anchor
#

Nest loads env files top down, so .env, then .env.whatever in your case, What variables belong to which file, because that may be making a difference here. You may want to swap the order that they are loaded

strange halo
#

What kind of order? by name?

nimble anchor
#

The order specified in the array

strange halo
#

i change the order of envFilePath but it did not effect it. what could i do wrong?

nimble anchor
#

Not sure off the top of my head. How are you checking the DATABASE_URL? Can you provide a reproduction?

strange halo
#

give me a sec

#
envFilePath: [
        `.env${process.env.NODE_ENV !== 'production' ? process.env.NODE_ENV : ''}`,
      ]

this code should be load .env.test during jest running

but it still loading .env

gaunt panther
#

are you using Prisma?

nimble anchor
#

Or nx

strange halo
gaunt panther
#

that's why, I guess

strange halo
#

But one of the problem still here. when i run the test. i also console log DATABASE_URL and it looks like this

mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}/${MONGO_DATABASE}?retryWrites=true&w=majority&authSource=admin&directConnection=true

But when i run the app. ${X} variables renders and console log correctly

nimble anchor
#

How are you logging them? process.env or configService.get()?

strange halo
#

process.env

nimble anchor
#

Where are you calling process.env? It may not be properly populated yet

strange halo
#

before jest init

#

but is it a correcy way to use right? an env variable inside another env variable?

nimble anchor
#

Persoanlly, I don't really like doing that. If I need to concat env variables, I'd do it in ym, code, not in the env file

strange halo
#

i don't like it too but prisma expecting this way

nimble anchor
#

Is it? Last I knew they just expected a DATABASE_URL. How you supply that is up to you

strange halo
#

i also wanted to produce it from another env variables inside env file

#
MONGO_HOST="localhost:27017"
MONGO_DATABASE="bookapp-test"
MONGO_USERNAME="root"
MONGO_PASSWORD="12345678"
DATABASE_URL=mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}/${MONGO_DATABASE}?retryWrites=true&w=majority&authSource=admin&directConnection=true
```

or should i direct put host,dbname etc. to the url?
nimble anchor
#

Do you have a reason to separate out the parts like that?

strange halo
#

i thought it would be easy to edit 🙂

nimble anchor
#

Do you need to edit them often?

strange halo
#

good point

#

should i remove this post or edit it to solved?

nimble anchor
#

General URL format <protocol>://<user>:<pass>@<host>:<port>/?<options> is pretty standard and easy enough to work with, in my opinion.

#

We'll mark it solved