11

Nextjs wants you to use a .env.local file to store env vars. Prisma uses .env

If I use a .env.local file then setting up the Prisma db

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

I get a DATABASE_URL does not exist error.

What's the right way to set up env vars for a Prisma, Nextjs, Vercel app?

grabury
  • 4,797
  • 14
  • 67
  • 125

3 Answers3

9

You can use dotenv-cli to force loading specific environment file.

1- Install dotenv-cli package

2- create script to run env before prisma migration on your package

"scripts": {
    ...
    "prismaDev": "dotenv -e .env.local prisma migrate dev ",
  }

3- now you can simply run npm run prismaDev

Chemi Adel
  • 1,964
  • 1
  • 10
  • 18
  • this is not working for me in Windows :( – Scaramouche Jul 18 '22 at 02:02
  • 1
    Why use an extra package?? Nextjs is already handling env vars, in a specific way at that. – David Aug 25 '22 at 07:16
  • 1
    @David I know that NextJS supports env files, but in the Author case is that he wants to run two env files. one for dev mode and second for live, so this why we need extra package for that https://www.prisma.io/docs/guides/development-environment/environment-variables/using-multiple-env-files – Chemi Adel Aug 25 '22 at 09:22
  • 1
    @ChemiAdel thats what I am saying, you dont need the extra module. you can just feed it the second file like .env - nextjs will read it after .env.local without needing the dotenv package seperately installed. I tried ;) If you want to seperate local from preview or prod, just dont add the new env var to your prod/preview env handler – David Aug 26 '22 at 06:30
  • 5
    Using `dotenv-cli` is what Prisma recommends in their documentation at https://www.prisma.io/docs/guides/development-environment/environment-variables/managing-env-files-and-setting-variables. The Prisma CLI only reads `.env` by default, but with Next.js you shouldn't store secrets in `.env`, but in `.env.local`. Next.js uses `dotenv` under the hood in its `@next/env` package, but you can't configure Prisma via CLI to utilize that unless you create your own script that uses `@next/env`. – chipit24 Oct 02 '22 at 00:25
  • @chipit24 is creating the script straightforward (like adding a script to package.json)? I don't know how to tweak Next "under the hood" but I'd also prefer to avoid installing redundant dependencies if possible. –  HigoChumbo Jan 03 '23 at 13:44
0

It's been a very long time since the question is asked but for people having the same problem:

1- In your vercel dashboard's project settings, open environment variables page. Then create the variable DATABASE_URL and assign to your database url.

2- On your local project run "vercel env pull". This will create a .env.local file in the root of your project containing your vercel deployments important values. Next.js is capable of loading multiple env files so you don't need to worry while running the project.

3- But I think node.js commands only load the .env file so when migrating you can use the following commands:

scripts: {
        "prismaMigrateLocal": "dotenv -e .env.local -- npx prisma migrate dev",
        "prismaMigrateLocalNoSeed": "dotenv -e .env.local -- npx prisma migrate dev --skip-seed"
}
Alper
  • 86
  • 4
-1

You can load environment variables with process.env.DATABASE_URL in your case and you can leave it in .env as Prisma asks for. Nextjs can handle multiple .env files without any extra effort.

No need to use an extra package, Nextjs will handle the env vars for you.

https://nextjs.org/docs/basic-features/environment-variables

https://nextjs.org/docs/basic-features/environment-variables#environment-variable-load-order

You can leave the standard setup and use the created .env file (by the prisma cli: https://www.prisma.io/docs/getting-started/setup-prisma) and add your connection string.

the rest is magically handled by next. Be sure to follow the instructions (manual or cli pointers) regarding gitignore and such..

To reiterate: both can live next to each other! Check the next documentation for load order if it matters. (see link above)

David
  • 425
  • 4
  • 9