question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting @prisma/client to work

See original GitHub issue

Trying to transition from mikro-orm to prisma this week, I immediately ran up against one issue mentioned in #841, specifically:

ERROR	{ Error: 
Invalid 'prisma.post.findMany()' invocation:

EROFS: read-only file system, chmod '/var/task/pages/api/query-engine-rhel-openssl-1.0.x'
    at PrismaClientFetcher.request (/var/task/pages/api/drafts.js:206:52123)
    at process._tickCallback (internal/process/next_tick.js:68:7) code: 'EROFS', meta: undefined }

After searching around, I saw many of the issues were either out of date (webpack configs don’t need to be touched anymore) or linked to solutions not meant for this repository. Quickly, I want to outline the extra two steps I took to make this work. As mentioned also in #841, this seems to be a problem with aws-lambda not respecting file permissions on upload, and thus not allowing the Prisma binary to be executed. To get around this, we can copy to /tmp before we run Prisma. (I wonder if there’s a way we can get a build.postDeploy step, where we can run a copy command instead of having to do these sorts of modifications inside the app?).

Step 1

I initialize Prisma once for my project in /config/prisma.js Inside there, I only had

import { PrismaClient } from "@prisma/client"

const Prisma = new PrismaClient();
export default Prisma;

To add the copy on app initialization, I modified it to be:

import { PrismaClient } from "@prisma/client";
import fs from "fs";
import { spawnSync } from "child_process";

if (process.env.NODE_ENV === "production") {
  const binaryPath = "/tmp/query-engine-rhel-openssl-1.0.x";

  if (!fs.existsSync(binaryPath)) {
    spawnSync("cp", [
      `${process.env.LAMBDA_TASK_ROOT}/node_modules/.prisma/client/query-engine-rhel-openssl-1.0.x`,
      "/tmp/",
    ]);

    spawnSync("chmod", [`555`, "/tmp/query-engine-rhel-openssl-1.0.x"]);
  }
}

const Prisma = new PrismaClient();
export default Prisma;

Step 2

This on it’s own does not work, because it is still looking in the wrong place for the binary. But as mentioned in the Prisma doc we can define custom paths for the binaries. Therefore I added

PRISMA_QUERY_ENGINE_BINARY = /tmp/query-engine-rhel-openssl-1.0.x

to my .env file

Finally, make sure you’ve read this guide and added this to your prisma/schema.prisma file

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "rhel-openssl-1.0.x"]
}

Conclusion

Im not sure if a build.postDeploy step (I believe the Vercel platform has this now to facilitate Prisma specifically) to allow copying of files or a change/patch to aws-lambda could mitigate these steps, but it feels sort of hacky to get Prisma working this way. If anyone has better solutions, I’d love to hear them.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:13

github_iconTop GitHub Comments

3reactions
millspcommented, May 10, 2021

I turns out that the binaries were not copied into the lambda. This could be solved by adding a script that triggers before upload. I have set a template up for prisma + serverless-next.js which adds a postBuildCommands (serverless.yml) script to do just that.

2reactions
cjoelruncommented, May 20, 2021

@millsp got something working. serverless-nextjs-js’s cdk construct doesn’t seem to have a way to have post command. Basically just manually move stuff after builder has finished.

import fse from "fs-extra"
import { join, resolve } from "path"

export const nextConfigDir = "."
export const outputDir = "./next-build"
new Builder(nextConfigDir, outputDir, {
  args: ["build"],
  env: {
    NODE_ENV: "development",
    NEXT_PUBLIC_API_URL: `https://${process.env.DOMAIN_NAME}`,
    NEXTAUTH_URL: `https://${process.env.DOMAIN_NAME}`,
  },
})
  .build(true)
  .then(async () => {
    const apiLambdaOutput = resolve(join(outputDir, "api-lambda"))
    const prismaDir = "node_modules/.prisma"
    const prismaDirSrc = resolve(join(nextConfigDir, prismaDir))
    const prismaDirDest = resolve(join(apiLambdaOutput, prismaDir))
    await fse.copySync(prismaDirSrc, prismaDirDest)
    /* Remove native query engine of workstation */
    await fse.remove(resolve(join(prismaDirDest, "client/query-engine-darwin")))
  })
  .then(() => {
    const stack = new NextJSStack(app, "NextJSStackBoilerPlate", {
      env: {
        region: "us-east-1",
        account: process.env.CDK_DEFAULT_ACCOUNT,
      },
      analyticsReporting: true,
      description: "NextJS Serverless CDK Construct Boilerplate",
      stackName: "nextjs-boilerplate",
    })

  })
  .catch(e => {
    console.error(e)
    process.exit(1)
  })```
Read more comments on GitHub >

github_iconTop Results From Across the Web

Prisma Client
The easiest way to get started with Prisma Client is by following the Quickstart. Quickstart (5 min). The setup instructions below provide a...
Read more >
@prisma/client - npm
You can use it as an alternative to traditional ORMs such as Sequelize, ... Follow one of these guides to get started with...
Read more >
Prisma Client Python
The Prisma schema · Data source: Specifies your database connection. In this case we use a local SQLite database however you can also...
Read more >
typescript - Property 'job' does not exist on type 'PrismaClient ...
I'm using Expressjs as a backend and also using Prisma to work with a database, and also using TypeScript ...
Read more >
Prisma | NestJS - A progressive Node.js framework
When setting up your NestJS application, you'll want to abstract away the Prisma Client API for database queries within a service. To get...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found