prisma: Missing `schema.prisma` in AWS Lambda layer

I’m testing prisma 3.0.2 and 3.1.1 with NPM v7 workspaces - It’s not running properly in my lambda function/layer setup. I get the error ENOENT: no such file or directory, open '/var/task/schema.prisma' Can I at least tell it what path my schema.prisma file is at somehow? I couldn’t find anything in the docs. I have the file in a lambda layer (/opt/nodejs/prisma)

Screenshot 2021-09-29 at 18 53 50

Additionally, it seems like in Prisma 3 it now is trying to find the schema.prisma file when I import the file that constructs my prisma client (new PrismaClient()), whereas before it didn’t do that. This is a problem for me because some of my lambda functions are set up to use prisma (with secrets, layer, network access to the DB, etc) and some aren’t but still import files which import the prisma client. So now I can’t start the functions that don’t have anything to do with prisma because it explodes saying it can’t find schema.prisma even though I have no intention of using it in that function.

    "stack": [
        "Error: ENOENT: no such file or directory, open '/var/task/schema.prisma'",
        "    at Object.openSync (fs.js:498:3)",
        "    at Object.readFileSync (fs.js:394:35)",
        "    at new LibraryEngine (/var/task/index.js:139195:46)",
        "    at PrismaClient.getEngine (/var/task/index.js:151600:20)",
        "    at new PrismaClient (/var/task/index.js:151569:33)",
        "    at packages/repo/build/client.js (/var/task/index.js:156432:18)",
        "    at __require (/var/task/index.js:38:45)",
        "    at packages/repo/build/index.js (/var/task/index.js:187999:20)",
        "    at __require (/var/task/index.js:38:45)",
        "    at packages/domain/build/auth/user/invite.js (/var/task/index.js:233728:27)"
    ]

Ideally the engine would lazily load the library engine when I actually need it and I could tell it where to find the schema prisma file.

Even better would be if it searched up from the node_modules dir (which lives in the layer) until it found a prisma dir and then looked in there for the schema file.

_Originally posted by @revmischa in https://github.com/prisma/prisma/issues/9435#issuecomment-930308899_

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 6
  • Comments: 21 (3 by maintainers)

Most upvoted comments

Hey everyone here. We are currently working through these errors and problems, and there is something we need to point out here before we dig in:

Prisma Client never tries to load your original schema.prisma file from where you generated the Client. That file is only relevant for CLI usage and when you run prisma generate. Later, the generated Client has a copy of the schema file that it reads and hands over to the Query Engine so it knows about the models and their fields and types. The ENOENT schema.prisma error message is about the Client not finding that file at the expected location (mostly right next to itself) when it starts.

(We could in theory inline the schema file into our JS code to avoid this error. Then the next line in our code that searches and loads the Query Engine would fail in exactly the same way. We need to fundamentally solve the underlying problem.)


That out of the way:

These problems are most probably caused by bundlers not copying the full generated Prisma Client to the new location and skipping the schema file, or rewriting/bundling/moving the code of Prisma Client in a way that the schema file copy is not in the expected location either.

Optimally each of you could provide a reproduction with a simplified version of your project, and post it into a new issue and then drop a link to that issue here. Currently, we already have a workaround for Monorepo + Next.js + Prisma that works. We are pretty confident we will also find one for others.

(I would be especially interesting in reproductions from @natevaughan @field123 @ptya @olup @kettanaito @Markkos89 @js-sapphire as those are combinations we are not aware of yet. The workaround mentioned in some of your answers should not be necessary.)

I’m running into this when using SvelteKit on Vercel and hitting a serverless function in SvelteKit’s api directory:

Error: ENOENT: no such file or directory, open '/var/task/schema.prisma'
    at Object.openSync (fs.js:497:3)
    at Object.readFileSync (fs.js:393:35)
    at new LibraryEngine (/var/task/index.js:48098:44)
    at PrismaClient.getEngine (/var/task/index.js:51308:20)
    at new PrismaClient (/var/task/index.js:51279:33)
    at Object.<anonymous> (/var/task/index.js:53931:12)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
 {  errno: -2,  syscall: 'open',  code: 'ENOENT',  path: '/var/task/schema.prisma',  clientVersion: '3.6.0'}

Everything works locally. Happy to provide more details or an example repo if helpful.

@natevaughan @field123 I also was facing this issue and copying over the postbuild script from this example solved the issue for me: https://github.com/mikenikles/sveltekit-prisma/blob/main/package.json

I am getting the same error auto-deploying to Vercel from an npm 7 workspace. Everything works fine locally. The install and generate steps work fine on Vercel. It’s a successful deploy. This only happens when executing the Vercel function.

I don’t want to step on @revmischa 's question if this isn’t much related. I’ll share more details if it’s useful.

[GET] / 21:19:10:31 2021-10-01T03:19:11.064Z
Error: ENOENT: no such file or directory, open '/var/task/schema.prisma'
    at Object.openSync (fs.js:498:3)
    at Object.readFileSync (fs.js:394:35)
    at new LibraryEngine (/var/task/index.js:35249:46)
    at PrismaClient.getEngine (/var/task/index.js:47687:20)
    at new PrismaClient (/var/task/index.js:47656:33) 
    at Object.<anonymous> (/var/task/index.js:50679:8)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10) 
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
 {  errno: -2,  syscall: 'open',  code: 'ENOENT',  path: '/var/task/schema.prisma',  clientVersion: '3.1.1'}

FWIW, I had to postinstall to get this far:

"postinstall": "npm i -D prisma@3.1.1 && prisma --version && prisma generate"

Experiencing the same issue with Turborepo + Remix + Prisma.

Adding the vercel-build or postbuild scripts from here does not help. I have the clients repo generating the Prisma client, but when it comes to building the frontend repo, the Prisma client is gone (cannot be required in the edge function).

That did not work. ⬆️ It did allow the Vercel function to load without error (so the site can load) but as soon as db.client is referenced, it just throws the same

{"errno":-2,"syscall":"open","code":"ENOENT","path":"/var/task/schema.prisma","clientVersion":"3.1.1"}