Skip to content

DATABASE_URL is required for prisma generate in v7 #28590

Open

Description

@jakub-msqt

We were generating prisma client in our CI pipeline in order to perform typechecking. After the update to v7, the pipeline breaks with Error: PrismaConfigEnvError: Missing required environment variable: DATABASE_URL.

I didn't see any mention of DATABASE_URL being now required for prisma generate command in the docs. I don't even see a reason why it should be.

Activity

  1. jakub-msqt commented on Nov 20, 2025

    @jakub-msqt
    Author

    Even after updating the Prisma package to version 7, the client may still be running version 6. Delete the generate folder completely and regenerate the client. Or you can also remove the prisma packages from node_modules and reinstall them, after generate new client

    I'm talking about CI pipeline. There is nothing to delete...

  2. iljamulders commented on Nov 20, 2025

    @iljamulders

    Same issue for us.

    The prisma.config.ts needs the env DATABASE_URL for migrations, but not for the generate command.

    For now, instead of the using the example from Prisma:

    import 'dotenv/config';
    import { defineConfig, env } from 'prisma/config';
    
    export default defineConfig({
        schema: 'prisma/schema.prisma',
        migrations: {
            path: 'prisma/migrations'
        },
        datasource: {
            url: env('DATABASE_URL') // <- the issue
        }
    });

    We just do:

    import 'dotenv/config';
    import { defineConfig } from 'prisma/config';
    
    export default defineConfig({
        schema: 'prisma/schema.prisma',
        migrations: {
            path: 'prisma/migrations'
        },
        datasource: {
            url: process.env.DATABASE_URL!
        }
    });
  3. jakub-msqt commented on Nov 20, 2025

    @jakub-msqt
    Author

    @iljamulders Nice one. I did a hot fix like: DATABASE_URL=test prisma generate, but yours is cleaner.

  4. YasserMountij commented on Nov 21, 2025

    @YasserMountij

    @iljamulders thanks dude for the solution , i dont see the need to require env when generating types

  5. anaelChardan commented on Nov 21, 2025

    @anaelChardan

    Same issue!

  6. jkomyno commented on Nov 24, 2025

    @jkomyno
    Contributor

    Hi @jakub-msqt, to clarify:

    • every prisma CLI command reads the Prisma config file (e.g., prisma.config.ts)
    • most commands use at least one of the possible attributes that can be set in prisma.config.ts
    • only some of these commands actually need the datasource.url value, especially prisma db *, prisma migrate *, and prisma generate --sql
    • prisma generate, per se, doesn't need any URL do be generated. However, if env() is given a non-defined env var, loading prisma.config.ts will result in a runtime error (you can see how env works here). And since any prisma commands loads it (including prisma generate), you're facing the error reported.

    @iljamulders has the right solution: #28590 (comment).
    If your environment variable isn't guaranteed to exist, you should not use the env() utility from prisma/config.

    We'll improve our docs to clarify this. Thanks!

  7. rovo89 commented on Nov 27, 2025

    @rovo89

    We'll improve our docs to clarify this. Thanks!

    That isn't done yet, is it? I'm wondering if there's a clever way to detect that the config is loaded. Then something like this would work to make the environment variable required exactly when needed:

    import 'dotenv/config';
    import { defineConfig, env, PrismaConfig } from 'prisma/config';
    
    function datasource(cb: () => PrismaConfig['datasource']) {
      const needsDatasource = false; // <--- How to determine if we need a datasource?
      return needsDatasource ? { datasource: cb() } : {};
    }
    
    export default defineConfig({
      schema: 'prisma/schema.prisma',
      migrations: {
        path: 'prisma/migrations',
      },
      ...datasource(() => ({
        url: env('DATABASE_URL'),
        shadowDatabaseUrl: process.env.SHADOW_DATABASE_URL,
      })),
    });

    or

    import 'dotenv/config';
    import { defineConfig, env } from 'prisma/config';
    
    const needsDatasource = false; // <--- How to determine if we need a datasource?
    
    export default defineConfig({
      schema: 'prisma/schema.prisma',
      migrations: {
        path: 'prisma/migrations',
      },
      datasource: needsDatasource
        ? {
            url: env('DATABASE_URL'),
            shadowDatabaseUrl: process.env.SHADOW_DATABASE_URL,
          }
        : undefined,
    });

    Of course the helper constant/function should be provided for this to be easily usable.

  8. leppaott commented on Nov 28, 2025

    @leppaott

    Command failed: prisma db execute --file ..migration.sql --url postgresql://post....." as a TypeScript/JavaScript module. Error: PrismaConfigEnvError: Missing required environment variable: DATABASE_URL

    Um db executefails too even though it uses --url

    Ok seems they dropped that flag but docs still have it: https://fig.io/manual/prisma/db/execute

  9. dbengtsson commented on Dec 1, 2025

    @dbengtsson

    I am facing issues with this as well.

    • The .env file with the connection string is gitignored causing the CI pipeline (Github Actions) for tests and builds to fail because it can't locate the file. Previously it was no issue because it would just read the env file during introspect.
    • Realized I don't need the .env file anymore when I use the new configuration as I can just create the connection string directly using our Google Cloud integration. Build now fails during generate because CI pipeline is not authenticated with the Google Cloud Project, which it shouldn't need except for during introspection.
  10. Zaeem6100 commented on Dec 2, 2025

    @Zaeem6100

    I have resolved this issue by providing the fake connection string in my git pipeline while building the container image. This resolves my problem.

    # Provide a safe placeholder DATABASE_URL at build-time so Prisma config can load
    # without requiring real credentials (only used for `prisma generate` during build).
    # You can override this at build with:
    #   docker build --build-arg PRISMA_GENERATE_DATABASE_URL="postgresql://user:pass@host:5432/db?schema=public" \
    #     -f Dockerfile.prod -t backend:latest .
    

    PRISMA_GENERATE_DATABASE_URL="postgresql://placeholder:placeholder@localhost:5432/placeholder?schema=public"

  11. SerenModz21 commented on Dec 2, 2025

    @SerenModz21

    I have resolved this issue by providing the fake connection string in my git pipeline while building the container image. This resolves my problem.

    # Provide a safe placeholder DATABASE_URL at build-time so Prisma config can load
    # without requiring real credentials (only used for `prisma generate` during build).
    # You can override this at build with:
    #   docker build --build-arg PRISMA_GENERATE_DATABASE_URL="postgresql://user:pass@host:5432/db?schema=public" \
    #     -f Dockerfile.prod -t backend:latest .
    

    PRISMA_GENERATE_DATABASE_URL="postgresql://placeholder:placeholder@localhost:5432/placeholder?schema=public"

    Note: the placeholder doesn't have to be a full Postgres URL. It can contain any value (e.g. DATABASE_URL=no).

    The env() helper does not validate the value; it just checks if it exists.

    export class PrismaConfigEnvError extends Error {
      constructor(name: string) {
        super(`Missing required environment variable: ${name}`)
        this.name = 'PrismaConfigEnvError'
      }
    }
    
    export function env<Env extends Record<string, string | undefined>>(name: keyof Env & string): string {
      const value = process.env[name]
      if (!value) {
        throw new PrismaConfigEnvError(name)
      }
      return value
    }

    Source: https://github.com/prisma/prisma/blob/main/packages/config/src/env.ts

  12. dbengtsson commented on Dec 2, 2025

    @dbengtsson

    Wouldn't just allowing the datasource-url to be a function resolve the issue? In that case the env file is not read except when needed, which in my case means I don't need the env file in the CI pipeline just because it needs to exist in the config as a string.

  13. added
    bug/1-unconfirmed
    on Dec 11, 2025
  14. 2 remaining items

  15. shimonenator commented on Dec 18, 2025

    @shimonenator

    Another way to look at this is the fact that only prisma migrate does not accept a custom db url via --url:

    • prisma generate does not require a datasource
    • prisma migrate needs a datasource
    • @prisma/client takes an instance of an adapter anyway

    Perhaps, at least for some of us, the solution is simply to add support for --url on prisma migrate and then remove datasource from the prisma.config.ts completely.

  16. AndresCasado commented on Feb 6, 2026

    @AndresCasado

    Hello, I'm dealing with DATABASE_URL right now and this issue seems to be the closest to mine.

    I'm containerizing two Prisma-powered apps, but there are opposing issues and solving one creates the other.

    Keep it as default

    If, as stated by the generated prisma.config.ts, the project has dotenv as a dev. dependency, then the npx prisma migrate deploy doesn't work when running the container because by loading the prisma.config.ts it will need dotenv, which is a dev. dependency (and because I'm deploying production, it is not installed).

    Solution for the container

    The container doesn't have dotenv, so if I remove the import from the prisma.config.ts file, then it works.

    The problem now is that running the npx prisma commands locally is cumbersome, as I don't have the environments variables defined in my machine (and as I'm developing diferent applications, it would drive me crazy to change for each of them manually)

    Ideas I have yet to try

    • Installing dotenv as a full dependency
      • It seems absurd to add a dependency to a container image just to NOT use it.
      • Also, as I researched on the .env file topic, I read a lot of advise against using dotenv or .env files in general and specially in production.
    • Containerize the development environment too.
      • It would technically work, but I feel it would render useless some features from my frameworks (like hot reloading).
    • Changing prisma.config.ts to, instead of using dotenv, checking if variables exist and, if they don't, load them from a .json file or similar.
      • I'm leaning towards this one because I feel it is "natural", but then I wonder why it is not the default.

    I feel like I might be missing something, because if dotenv is supposed to be a dev. dependency maybe I should not be trying to run npx prisma migrate deploy from production, but I've thought of all the possibilities and can not reach a conclussion by myself.

    Any help is appreciated. Thanks.

  17. domvanrob commented on Feb 6, 2026

    @domvanrob

    @AndresCasado

    I ended up changing my Prisma config to a .js file, so it doesn't need to be compiled. This, combined with not using the env() method, but just using process.env.DATABASE_URL.

    Hope this helps.

  18. ionkorol commented on Feb 6, 2026

    @ionkorol

    When I run prisma generate --sql to generate typed sql it's worse. It requires the datasource.url and it needs it to be a proper connection string because it actually connects to the database. Not sure why that was the design choice and not just generate based on the schema files.

  19. AndresCasado commented on Feb 7, 2026

    @AndresCasado

    @AndresCasado

    I ended up changing my Prisma config to a .js file, so it doesn't need to be compiled. This, combined with not using the env() method, but just using process.env.DATABASE_URL.

    Hope this helps.

    Sorry, but I can't see how it would help. I'm already not using the env() method, but if the dependency to dotenv remains, what does it matter if it is compiled or not?

  20. domvanrob commented on Feb 9, 2026

    @domvanrob

    I was missing the config file in my final Docker runtime stage. This didn't have tsc or tsx to compile to the .ts Config. Although, I'm not a 100% sure this was the issue as I also had a typo in the path where I copied the config to 🤡

  21. karthicedric7-cloud commented on Mar 20, 2026

    @karthicedric7-cloud

    This is exactly what safelaunch catches it validates that DATABASE_URL and all your other required env variables are present before you run any commands or push to production. Would have flagged this immediately.

    npm install -g safelaunch

    https://github.com/karthicedric7-cloud/safelaunch

  22. karthicedric7-cloud commented on Mar 21, 2026

    @karthicedric7-cloud

    Just shipped git hook support too safelaunch hook install blocks your push automatically if any env check fails. No more manual running.

    https://www.npmjs.com/package/safelaunch

  23. AndresCasado commented on Mar 23, 2026

    @AndresCasado

    This is exactly what safelaunch catches it validates that DATABASE_URL and all your other required env variables are present before you run any commands or push to production. Would have flagged this immediately.

    npm install -g safelaunch

    https://github.com/karthicedric7-cloud/safelaunch

    This doesn't address the core problem of the issue, which is that generating code with prisma generate should not need to have the env. variable defined

  24. dizzyjaguar commented on Apr 2, 2026

    @dizzyjaguar

    any update on this??

  25. bcg-kandora commented on Jul 27, 2026

    @bcg-kandora

    With Prisma 7.8.0 (and maybe earlier) datasource.url may in fact be undefined so you can just use process.env.DATABASE_URL without any compile errors. If DATABASE_URL is not defined

    • npx prisma generate works fine (as desired), and
    • npx prisma migrate dev properly reports

      Error: The datasource.url property is required in your Prisma config file when using prisma migrate dev.

    I'd argue this issue is solved while the proposed usage of env("DATABASE_URL") in the docs and migration guide is still misleading.


    In my case VS Code/LSP still reports error

    Cannot find name 'process'. Do you need to install type definitions for node? Try npm i --save-dev @types/node and then add 'node' to the types field in your tsconfig.ts(2591)

    since my tsconfig.json contains "include": ["src/**/*"],. This is not related to Prisma though, and npx prisma generate works nonetheless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/1-unconfirmedkind/bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions