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.

"Environment variables loaded from .env" is misleading

See original GitHub issue

Bug description

I followed the “Running migrations on different environments” guide and executed this statement in my terminal:

dotenv -e .env.test -- npx prisma migrate dev

It was giving me the following output:

Environment variables loaded from .env Prisma schema loaded from prisma\schema.prisma Datasource “db”: PostgreSQL database “tests”, schema “public” at “localhost:5433”

I was very surprised because I do have a .env and .env.test file in my project and thought that Pisma is not pickung up my .env.test file because it’s reporting that it loaded variables from “.env”. In fact it used my .env.test file.

How to reproduce

  1. Create a .env file with DATABASE_URL="postgresql://postgres:postgres@localhost:5432/root_db?schema=public"
  2. Create a .env.test file with DATABASE_URL="postgresql://prisma:prisma@localhost:5433/test_db?schema=public"
  3. Run dotenv -e .env.test -- npx prisma migrate dev

Expected behavior

To avoid this confusion I suggest to change the phrasing of the console output. Preferably it should show the correct file name (e.g. loaded from .env.test) or not mention the file name at all (e.g. loaded environment variables).

Prisma information

schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model enum_role {
    type        String @id
    description String
}

Environment & setup

  • OS: Windows 10
  • Database: PostgreSQL
  • Node.js version: 16.10.0

Prisma Version

prisma                  : 3.4.0
@prisma/client          : 3.4.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at ..\..\node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine        : migration-engine-cli 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at ..\..\node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine    : introspection-core 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at ..\..\node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85 (at ..\..\node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash    : 1c9fdaa9e2319b814822d6dbfd0a69e1fcc13a85
Studio                  : 0.438.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bennycodecommented, Nov 19, 2021

Hi @janpio, your explanation makes sense to me. I think the best that can be done here is placing a note in the docs. And how about rephrasing “Environment variables loaded from .env” to just “Environment variables loaded”?

0reactions
NTagcommented, Nov 10, 2022

It’s super misleading, I lost two hours trying to figure out why my env variables from .env file were loaded during my tests whereas I followed the instructions to load a .env.test file.

Actually dotenv and prisma aren’t smart at all: if you have the same variable in the two files and used dotenv -e .env.test ok when prisma is initiated it won’t override the variable. HOWEVER, it will load variables from the .env file which aren’t in the .env.test file. This is super wrong, this is not explained in the docs, and this is a very bad pattern.

.env file shouldn’t be automatically magically loaded by prisma.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 reasons why your .env environment variables don't work
1. Your framework doesn't automatically load .env files. ... While some frameworks and packages come built-in support for environment variables ...
Read more >
Problems with Environment Variables
Environment variable is not set. If the error message states that the environment variable is not set, IT MEANS PRECISELY THAT ! The...
Read more >
dotenv file is not loading environment variables - Stack Overflow
This fixed my problem with "undefined" .env variables. I even have another project running with the same exact setup for configuring server and...
Read more >
Resolution of environment variables - Serverless Framework
Differences against serverless-dotenv-plugin · The framework only loads environments variables locally and does not pass them to your function's environment · The ...
Read more >
Environment Variables: What They Are and How To Use Them
env files in a local development environment. Unlike platform-native variable managers, you do not need to deploy your app to leverage the ...
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