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

UnhandledSchemeError: cloudflare when Using postgres with Cloudflare Workers and Next.js Edge Runtime #930

Open
jacksonkasi1 opened this issue Aug 16, 2024 · 2 comments

Comments

@jacksonkasi1
Copy link

jacksonkasi1 commented Aug 16, 2024

Hi,

I'm encountering an issue while trying to deploy my Next.js API routes using the postgres package with Drizzle ORM on Cloudflare Workers. Everything works fine locally, but when I set export const runtime = "edge"; for the edge runtime in Next.js, I get the following error during compilation:

⨯ cloudflare:sockets
Module build failed: UnhandledSchemeError: Reading from "cloudflare:sockets" is not handled by plugins (Unhandled scheme).
Webpack supports "data:" and "file:" URIs by default.
You may need an additional plugin to handle "cloudflare:" URIs.
Import trace for requested module:
cloudflare:sockets
./node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cf/polyfills.js
./node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cf/src/index.js
./src/db/index.ts
./src/app/api/[[...route]]/routes/register/index.ts
./src/app/api/[[...route]]/routes/index.ts
./src/app/api/[[...route]]/route.ts

Setup:

  • Framework: Next.js 14
  • Database: PostgreSQL
  • ORM: Drizzle ORM with the postgres package
  • Runtime: edge (Cloudflare Workers)

Code Snippet:

import { env } from "@/config/env";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";

import * as schema from "./schema";

const client = postgres(env.DATABASE_URL);
export const db = drizzle(client, { schema, logger: true });

Issue Summary:

The error seems to originate from the postgres package when trying to use it with the edge runtime on Cloudflare Workers. It looks like Webpack doesn't know how to handle the cloudflare: scheme used in postgres/cf/polyfills.js.

Questions:

  1. Is this a known issue with the postgres package when used in Cloudflare Workers with the edge runtime?
  2. Are there any recommended workarounds or configurations that could resolve this issue?

Any guidance or suggestions would be greatly appreciated!

Thanks!

@archodev
Copy link

Same issue, did you manage to fix this?

@jacksonkasi1
Copy link
Author

Hi @archodev.

To avoid this issue, we recently switched postgres to @neondatabase/serverless.
@neondatabase/serverless works well with the edge runtime and Next.js API.

But if I execute any script with the DB in a separate file, it gives some issues.

You can check out this repo, it's simple code: https://github.com/jacksonkasi1/drizzle-form

Also, you can reproduce the issue. I have commented code, check out: src/db/index.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants