You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `env()` helper function from `prisma/config`**throws an error** if the specified environment variable is not defined. This is important to understand because:
632
+
633
+
- Every Prisma CLI command loads the `prisma.config.ts` file
634
+
- Only **some** commands actually need the `datasource.url` value (e.g., `prisma db *`, `prisma migrate *`, `prisma generate --sql`)
635
+
- Commands like `prisma generate` don't need a database URL, but will still fail if `env()` throws an error when loading the config file
636
+
637
+
For example, if you run `prisma generate` without `DATABASE_URL` set, and your config uses `env('DATABASE_URL')`, you'll see:
**Solution:** If your environment variable isn't guaranteed to exist (e.g., in CI/CD pipelines where you only run `prisma generate` for type-checking), don't use the `env()` helper. Instead, access the environment variable directly:
644
+
645
+
```ts
646
+
import'dotenv/config'
647
+
import { defineConfig } from"prisma/config";
648
+
649
+
exportdefaultdefineConfig({
650
+
schema: 'prisma/schema.prisma',
651
+
migrations: {
652
+
path: 'prisma/migrations',
653
+
},
654
+
datasource: {
655
+
url: process.env.DATABASE_URL!, // Or use: process.env.DATABASE_URL ?? '' to provide a fallback value
656
+
},
657
+
});
658
+
```
659
+
660
+
:::note
661
+
662
+
Use the `env()` helper when you want to **enforce** that an environment variable exists. Use `process.env` directly when the variable may be optional depending on the command being run.
663
+
664
+
:::
665
+
629
666
### Using multi-file schemas
630
667
631
668
If you want to split your Prisma schema into multiple files, you need to specify the path to your Prisma schema folder via the `schema` property:
0 commit comments