question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Switch `.env` file based on `NODE_ENV`

See original GitHub issue

This issue depends on: https://github.com/prisma/prisma/issues/3720.

Problem

We don’t offer a native solution for switching environments when running the CLI.

A good illustration of this problem is if you want to migrate your test database, you need to run prisma migrate with your test environment.

Workaround

The current workarounds are to either provide the environment variables explicitly:

DATABASE_URL="db_test_url" prisma migrate

Or call your environment loader from the outside:

dotenv -e env.test -- prisma migrate

Or call the Prisma CLI from another script:

await execa("prisma", ["migrate"], { env: customEnv })

Proposal

Blitz, Next.js, Create React App support .env.*. This approach is nicely summed up on Next.js’s documentation

In general only one .env.local file is needed. However, sometimes you might want to add some defaults for the development (next dev) or production (next start) environment.

Next.js allows you to set defaults in .env (all environments), .env.development (development environment), and .env.production (production environment).

.env.local always overrides the defaults set.

Note: .env, .env.development, and .env.production files should be included in your repository as they define defaults. .env*.local should be added to .gitignore, as those files are intended to be ignored. .env.local is where secrets can be stored.

Source: https://nextjs.org/docs/basic-features/environment-variables#default-environment-variables

Concerns

We’re hesitant to implement this because:

  • It’s a very specific solution to environment management. There’s a risk that this approach will change over time.
  • Adds some runtime complexity to the client
  • We want to make sure we support other environment loaders. For example, https://github.com/99designs/aws-vault.
    • This should already work, just need to test it.

Related Issues and Discussion

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:83
  • Comments:19 (3 by maintainers)

github_iconTop GitHub Comments

66reactions
NMinhNguyencommented, Oct 16, 2020

Would be great if .env.local was supported out of the box (I don’t think that this particular feature needs to be opted in to).

17reactions
nephixcommented, Jan 26, 2021

First problem is that env vars need loaded from both .env and .env.production in this case and merged together.

I was able to make this work with dotenv-flow and dotenv-flow-cli and then in my package.json:

"db:reset": "dotenv-flow -- npx prisma migrate reset --preview-feature",
"db:migrate": "dotenv-flow -- npx prisma migrate dev --preview-feature",
"db:deploy": "dotenv-flow -- npx prisma migrate deploy --preview-feature",

It loads the main .env file and merges it with overwrites from e.g. .env.local or .env.production

Read more comments on GitHub >

github_iconTop Results From Across the Web

Toggle between multiple .env files like .env.development with ...
I want to use separate .env files for each mode (development, production, etc...). When working on my vue.js projects, I can use files...
Read more >
Customizing Node.js .env files - LogRocket Blog
We'll then demonstrate how to use multiple customized .env files to quickly switch between multiple environment-specific configurations.
Read more >
Node JS Environment Variables — Setting Node app for ...
In this Blog we will see how we can setup custom .env files for our node ... import the config.js file and run...
Read more >
How to customize Node.js .env files for different environment ...
This file needs a something like a parser to make it work. The parser reads the variable definitions one-by-one and parses them to...
Read more >
Working with Environment Variables in Node.js - Twilio
env files. There's also an alternative module based on dotenv to make loading environment variables more convenient. The result is node-env-run ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found