P
Prisma2mo ago
PDDO

Issue with Prisma D1 Adapter and Cloudflare Workers: fs.readdir Not Implemented

I’m currently building a website using Next.js, and I am using Prisma ORM to manage a Cloudflare D1 database. However, I’ve encountered an issue. Previously, when I was using Prisma to manage a PostgreSQL database with Next.js on Cloudflare Workers, everything worked perfectly. Recently, I wanted to switch to using Cloudflare’s D1 database, but I’ve run into a problem.
4 Replies
Prisma AI Help
Prisma AI Help2mo ago
Skipping coffee for instant knowledge! Let's brew that AI answer now.
PDDO
PDDOOP2mo ago
To use Next.js with Cloudflare Workers, I need to use the @opennextjs/cloudflare package. My current setup looks like this:
import { PrismaClient } from "@/lib/prisma";
import { getCloudflareContext } from "@opennextjs/cloudflare";
import { PrismaD1 } from "@prisma/adapter-d1";

const ctx = await getCloudflareContext({ async: true });
const prisma = new PrismaClient({
adapter: new PrismaD1(ctx.env.D1),
});

export default prisma;
import { PrismaClient } from "@/lib/prisma";
import { getCloudflareContext } from "@opennextjs/cloudflare";
import { PrismaD1 } from "@prisma/adapter-d1";

const ctx = await getCloudflareContext({ async: true });
const prisma = new PrismaClient({
adapter: new PrismaD1(ctx.env.D1),
});

export default prisma;
However, when I initialize the Prisma client, I get the following error:
[ERROR] ⨯ Error: [unenv] fs.readdir is not implemented yet!

at createNotImplementedError
(Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:38:10)
at Object.fn [as readdir]
(Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:43:27)
at e8 (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:141004:40)
at e9 (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:140997:31)
at e5 (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:140978:37)
at async e4 (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:140940:711)
at async tn (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:141024:23)
at async te (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:141012:41)
at async ol.getCurrentBinaryTarget
(Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:144352:27)
at async ol.instantiateLibrary
(Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:144347:41) {
clientVersion: '6.7.0',
digest: '1945151018'
}
[ERROR] ⨯ Error: [unenv] fs.readdir is not implemented yet!

at createNotImplementedError
(Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:38:10)
at Object.fn [as readdir]
(Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:43:27)
at e8 (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:141004:40)
at e9 (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:140997:31)
at e5 (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:140978:37)
at async e4 (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:140940:711)
at async tn (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:141024:23)
at async te (Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:141012:41)
at async ol.getCurrentBinaryTarget
(Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:144352:27)
at async ol.instantiateLibrary
(Users/zhe/Documents/vve/lab/.wrangler/tmp/dev-b2syq6/worker.js:144347:41) {
clientVersion: '6.7.0',
digest: '1945151018'
}
Previously, when using Prisma, I would run bun prisma generate --no-engine to generate the client, but with the adapter, this command is not allowed. Now, I can only run bun prisma generate to generate the client, but Cloudflare Workers do not support the fs.readdir API. I’m wondering if anyone has encountered this issue or can point me in the right direction to solve this problem. Any help would be greatly appreciated!
Prisma AI Help
Prisma AI Help2mo ago
Based on the knowledge sources, there are several known issues when using Prisma with Cloudflare D1, particularly in edge environments like Cloudflare Workers. The error you're encountering with fs.readdir Not Implemented is likely related to how Prisma tries to access the filesystem in edge environments. This is a common issue because edge runtimes like Cloudflare Workers don't have full Node.js filesystem access. There are a few potential solutions: 1. Use the latest Prisma version with driver adapters: Make sure you're using Prisma v5.16.0 or newer with the D1 adapter properly configured:
import { PrismaClient } from "@prisma/client";
import { PrismaD1 } from "@prisma/adapter-d1";

// In your Worker
const adapter = new PrismaD1(env.DB);
const prisma = new PrismaClient({ adapter });
import { PrismaClient } from "@prisma/client";
import { PrismaD1 } from "@prisma/adapter-d1";

// In your Worker
const adapter = new PrismaD1(env.DB);
const prisma = new PrismaClient({ adapter });
2. Check your schema.prisma configuration: Ensure you have the proper configuration:
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}

datasource db {
provider = "sqlite"
url = "file:./dev.db" // This is ignored at runtime with D1
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}

datasource db {
provider = "sqlite"
url = "file:./dev.db" // This is ignored at runtime with D1
}
3. Externalize Prisma in your build: If you're using Vite to build your Cloudflare Pages project, you might need to externalize Prisma during the build process as mentioned in this GitHub issue:
// vite.config.ts
export default defineConfig({
ssr: {
external: ['@prisma/client', '@prisma/adapter-d1'],
},
// rest of your config
});
// vite.config.ts
export default defineConfig({
ssr: {
external: ['@prisma/client', '@prisma/adapter-d1'],
},
// rest of your config
});
4. Memory leak issues: Be aware that there are reported memory leak issues when using driver adapters with Cloudflare D1. If you continue to face issues, you might want to consider using Prisma Accelerate as an alternative approach, which is specifically designed for edge environments. 🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly. In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.
Nurul
Nurul2mo ago
I assume this discord conversation is related to this discussion? https://github.com/prisma/prisma/discussions/27041
GitHub
Issue with Prisma D1 Adapter and Cloudflare Workers: fs.readdir Not...
Question I’m currently building a website using Next.js, and I am using Prisma ORM to manage a Cloudflare D1 database. However, I’ve encountered an issue. Previously, when I was using Prisma to man...

Did you find this page helpful?