Louis Joubert
07/06/2022, 7:51 AMimport { PrismaClient } from "@prisma/client";
console.log(process.env.TEST);
will pull TEST
from my .env
file?
Earlier the docs say When you use Prisma CLI or Prisma Client, the .env file content and the variables defined in there are put into the system's environment, where Prisma can read it and use it.
When it says using Prisma Client, does that mean using PrismaClient in your code, as opposed to using some CLI command?
I would prefer that it doesn't pull in stuff automatically, but I guess that just means I should not have any stray .env files lying around.PinkiePie
07/06/2022, 8:09 AM.env
file by default, you can use this 3rd party module to load it: https://www.npmjs.com/package/dotenv
but you don't have to use env at all, you can pass database URL manually:
export const prisma = new PrismaClient({
datasources: { db: { url: 'my url' } },
});
Louis Joubert
07/06/2022, 8:21 AM.env
file using any 3rd party module, just importing the PrismaClient injects the .env
content into my environment?PinkiePie
07/06/2022, 8:59 AMRichard Ward
07/06/2022, 11:06 AM.env
file for the schema.prisma
file.
file for theRichard Ward
07/06/2022, 11:07 AM<http://process.env.xxx|process.env.xxx>
in your node application.
If you wanted to have access to items in your .env
file in your node code then you would need to use something like dotenv
Louis Joubert
07/06/2022, 3:26 PM.env
into my environment, whether I want it or not ...
So I start with just Prisma installed, a basic .env
file, and a basic model, and generate the database and client ...
> cat package.json
{
"name": "test",
"devDependencies": {
"prisma": "^4.0.0"
},
"dependencies": {
"@prisma/client": "^4.0.0"
},
"type": "module"
}
> cat .env
DATABASE_URL=file:./whatever.db
TEST=well hello there!
> cat schema.prisma
// This is your Prisma schema file,
// learn more about it in the docs: <https://pris.ly/d/prisma-schema>
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
}
> npx prisma db push
Environment variables loaded from .env
Prisma schema loaded from schema.prisma
Datasource "db": SQLite database "whatever.db" at "file:./whatever.db"
The database is already in sync with the Prisma schema.
ā Generated Prisma Client (4.0.0 | library) to ./node_modules/@prisma/client in 361ms
I verify that I don't have TEST
defined anywhere in my environment ...
> env | grep TEST
>
I create a simple app that just imports the PrismaClient (no dotenv, nothing) ...
> cat index.js
import { PrismaClient } from "@prisma/client";
console.log(process.env.TEST);
And when I run it, I expect it to print out undefined
, but I get this ...
> node index
well hello there!
PinkiePie
07/06/2022, 3:56 PMLouis Joubert
07/07/2022, 6:24 AMChris Tsongas
07/07/2022, 5:37 PMdotenv-flow
to manage variables for different environments however I abandoned my attempt at that as untenable due to Prisma's poor design decision IMO to load variables from a .env
file.PinkiePie
07/07/2022, 6:39 PM.env
file without polluting process.env
. @nikolasburk is it a feature? Or should we maybe report it as a bug?Chris Tsongas
07/07/2022, 6:46 PM.env
file for you.Chris Tsongas
07/07/2022, 6:50 PMdotenv-flow
.Richard Ward
07/07/2022, 7:14 PMDATABASE_URL
from the .env
(via some form of transversal) and not that it loaded everything. This doesn't seem too desirable..