Skip to content

Switch .env file based on NODE_ENV #3865

@matthewmueller

Description

@matthewmueller

This issue depends on: #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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions