DEV Community

[Comment from a deleted post]
Collapse
 
shontauro profile image
Fabian Leon

It only works for one environment, what about if you have other environments such .env.development .env.staging .env.production etc

Collapse
 
v4irajvimu profile image
Viraj Vimukthi Jayasinghe

did you found the answer?

Collapse
 
lamine105 profile image
lamine

take a look at dotenv-flow

Collapse
 
cullylarson profile image
Cully Larson

I did something like this in next.config.js:

if(process.env.NODE_ENV === 'development') {
    require('dotenv').config({'path': path.join(__dirname, 'secrets/app.dev.env')})
}
else if(process.env.NODE_ENV === 'production') {
    require('dotenv').config({'path': path.join(__dirname, 'secrets/app.prod.env')})
}

Next.js sets process.env.NODE_ENV depending on the build environment. You could potentially also set another environment variable in your package.json like this:

    "dev": "CONFG_ENV=development next",
    "build": "CONFG_ENV=production next build",
    "start": "CONFG_ENV=production next start",

Then in your next.config.js you'd refernece CONFIG_ENV instead of NODE_ENV.

Collapse
 
chuckjonas profile image
Charlie Jonas

hopefully you weren't following the solution above and passing your application secrets into DefinePlugin...

 
cullylarson profile image
Cully Larson

Haha, there aren't actually any secrets in that folder 😁 I just always have a folder with that name that I gitignore and put things I don't want in the repo in there (config, actual secrets, etc). Totally slipped my mind it was named that here.