Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prisma edge client can't read env vars from .env file #18542

Closed
kelbyfaessler opened this issue Mar 28, 2023 · 15 comments
Closed

Prisma edge client can't read env vars from .env file #18542

kelbyfaessler opened this issue Mar 28, 2023 · 15 comments
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: data proxy topic: .env topic: @prisma/client/edge

Comments

@kelbyfaessler
Copy link

kelbyfaessler commented Mar 28, 2023

Bug description

When I import "@prisma/client" it reads my env vars (e.g. DATABASE_URL).

Using the same .env file, when I import "@prisma/client/edge" it can't read them anymore and I get
InvalidDatasourceError: Datasource "db" references an environment variable "DATABASE_URL" that is not set

This is the local dev server for a sveltekit app, so not even trying to deploy it yet.

The schema code that reads env looks like this:

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
  // directUrl = env("DIRECT_URL")
  relationMode = "prisma"
}

How to reproduce

Create sveltekit skeleton project
Verify it works
Add prisma, a schema (reads DATABASE_URL from env) with a table, and a row of data (mysql planetscale db)
Add a .env file with the database url string
Verify you can query the database and run the local dev server, see the data in the page
Change to the prisma edge client
You'll see the error above

Expected behavior

No response

Prisma information

Relevant parts of schema already provided

Environment & setup

  • OS: Fedora
  • Database: PlanetScale
  • Node.js version: 18.7.0

Prisma Version

4.10.0
@kelbyfaessler kelbyfaessler added the kind/bug A reported bug. label Mar 28, 2023
@kelbyfaessler
Copy link
Author

I wonder if this is related to #15958

@kelbyfaessler
Copy link
Author

I don't even necessarily need a solution to this problem (although I'll take one if you have it). I just need some ways to debug what is going wrong because right now I don't have any ideas besides reading prisma source code, since that's most of the stacktrace.

Speaking of stacktrace, here it is:

InvalidDatasourceError: Datasource "db" references an environment variable "DATABASE_URL" that is not set
    at Ar.resolveDatasourceURL (<myproject>/node_modules/@prisma/client/runtime/edge.js:25:8662)
    at Ar.extractHostAndApiKey (<myproject>/node_modules/@prisma/client/runtime/edge.js:25:7743)
    at new DataProxyEngine (<myproject>/node_modules/@prisma/client/runtime/edge.js:25:3535)
    at t.getEngine (<myproject>/node_modules/@prisma/client/runtime/edge.js:88:3273)
    at new PrismaClient (<myproject>/node_modules/@prisma/client/runtime/edge.js:88:2867)
    at <myproject>/src/lib/server/prisma.ts:4:34
    at async instantiateModule (file://<myproject>/node_modules/vite/dist/node/chunks/dep-3007b26d.js:52400:9)

@kelbyfaessler
Copy link
Author

I just created a new sveltekit skeleton project, added prisma with a single table in the schema, confirmed I could query the database (planetscale) with an actual row of data, then switched to the prisma edge client and it stopped working with the same env var error message above.

This definitely appears to be a prisma issue with sveltekit/planetscale

@jkomyno
Copy link
Contributor

jkomyno commented Apr 3, 2023

Hi @kelbyfaessler, this issue is indeed a duplicate of #15958, so I would close this new issue and redirect future communications there.

This seems to be one possible workaround, but we're currently looking for better solutions.

@jkomyno jkomyno closed this as completed Apr 3, 2023
@kelbyfaessler
Copy link
Author

Hi @jkomyno, would you mind sharing your reasoning for why you think this is a duplicate?

@kelbyfaessler
Copy link
Author

A minimal example that reproduces the issue:

https://github.com/kelbyfaessler/prisma-env-var

@janpio
Copy link
Member

janpio commented Apr 3, 2023

I see no connection to Cloudflare either.

The edge Client does not load the .env file. Does it work if you manually set the environment variable? Or load it via dotenv-cli first when starting your server?

@kelbyfaessler
Copy link
Author

@janpio it works if I pass the env var to the client constructor like in the CF issue thread. Not sure why that would make a difference, especially for local dev, but it does.

Still trying to get deployment to vercel edge functions working (also has errors)

@janpio janpio reopened this Apr 6, 2023
@janpio janpio added bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. topic: @prisma/client/edge labels Apr 6, 2023
@jkomyno jkomyno added the team/client Issue for team Client. label Apr 6, 2023
@kelbyfaessler
Copy link
Author

The deployment to vercel edge functions now works with the same solution of passing database url env var to the prisma client constructor like this:

import { DATABASE_URL } from "$env/static/private";

  new PrismaClient({
    datasources: {
      db: {
        url: DATABASE_URL
      }
    }
  });

But the issue remains for why it's necessary to do that when switching to the edge client

@ticup
Copy link

ticup commented May 1, 2023

I had the same problem, the proposed workaround works, thanks.

@miguelff miguelff added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Jul 17, 2023
@Danubius1st

This comment was marked as outdated.

@janpio janpio changed the title Prisma edge client can't read env vars Prisma edge client can't read env vars from .env file Jul 28, 2023
@janpio

This comment was marked as outdated.

@janpio
Copy link
Member

janpio commented Aug 7, 2023

@kelbyfaessler I took another look at this, and the explanation is much simpler:

  • @prisma/client loads the .env file and makes its values available as environment variables.
  • @prisma/client/edge is designed for edge environments where no files can be accessed or read, so it does not try to interact with the .env file at all.

So when using @prisma/client/edge in your app you have to load the environment variables some other way, and they will be available in your code:

  • In deployment platforms like Lambda, Netlify or Vercel Serverless Functions you can define the environment variables via some UI - and they are then available in process.env to your app, which Prisma will read.
  • In some other deployment environments like Cloudflare Workers (with the module workers) you do not get the environment variable values you defined in process.env, but in e.g. a param of your handler. In those cases you need to programmatically override the url in the PrismaClient() constructor.
  • And finally, locally or in other environments where you don't have a nice UI, you need to set it outside of your app for the whole system, or use something like dotenv-cli before you execute your app.

That means that on Vercel Edge Functions for example the workaround you posted above should not be necessary.

@janpio
Copy link
Member

janpio commented Aug 7, 2023

And to show with your reproduction repository you provided (Thank you!):

gitpod /workspace/prisma-env-var (main) $ npm run dev

> prisma-env-var@0.0.1 dev
> vite dev


Forced re-optimization of dependencies

  VITE v4.2.1  ready in 721 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help
5:44:38 PM [vite] Error when evaluating SSR module /src/lib/server/prisma.ts:

5:44:38 PM [vite] Error when evaluating SSR module /src/routes/+page.server.ts:

Internal server error: Datasource "db" references an environment variable "DATABASE_URL" that is not set
      at Rr.resolveDatasourceURL (/workspace/prisma-env-var/node_modules/@prisma/client/runtime/edge.js:25:8817)
      at Rr.extractHostAndApiKey (/workspace/prisma-env-var/node_modules/@prisma/client/runtime/edge.js:25:7898)
      at new DataProxyEngine (/workspace/prisma-env-var/node_modules/@prisma/client/runtime/edge.js:25:3637)
      ...

gitpod /workspace/prisma-env-var (main) $ npm install -g dotenv-cli

added 10 packages in 1s

2 packages are looking for funding
  run `npm fund` for details
gitpod /workspace/prisma-env-var (main) $ dotenv npm run dev

> prisma-env-var@0.0.1 dev
> vite dev



  VITE v4.2.1  ready in 715 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help
5:45:12 PM [vite] Error when evaluating SSR module /src/lib/server/prisma.ts:

5:45:12 PM [vite] Error when evaluating SSR module /src/routes/+page.server.ts:

Internal server error: Datasource URL must use prisma:// protocol when --data-proxy is used
      at Rr.extractHostAndApiKey (/workspace/prisma-env-var/node_modules/@prisma/client/runtime/edge.js:25:8104)
      ...

First it fails as you describe. Then I install dotenv-cli and use it before the start command - and it complains that the DATABASE_URL is mysql:/... instead of prisma://... 😆

And if I set the value in .env to DATABASE_URL=prisma://foo?api_key=123 we get this:

gitpod /workspace/prisma-env-var (main) $ dotenv npm run dev

> prisma-env-var@0.0.1 dev
> vite dev



  VITE v4.2.1  ready in 741 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h to show help
Error: 
Invalid `prisma.star.findMany()` invocation:


Cannot fetch data from service:
getaddrinfo ENOTFOUND foo
    at Yn.handleRequestError (/workspace/prisma-env-var/node_modules/@prisma/client/runtime/edge.js:88:6681)
    at Yn.handleAndLogRequestError (/workspace/prisma-env-var/node_modules/@prisma/client/runtime/edge.js:88:6110)
   ... 

So it definitely gets the correct value with the simple, unmodified new PrismaClient().

@janpio
Copy link
Member

janpio commented Aug 7, 2023

(Closing this issue now as @prisma/client/edge works as expected - we will still try to improve this situation of course)

(@kelbyfaessler @ticup Please still let me know if you agree and understand all of this! I just close the issue so we know which ones we actually have to fix something, not to get rid of you.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: data proxy topic: .env topic: @prisma/client/edge
Projects
None yet
Development

No branches or pull requests

6 participants