porsager / porsager/postgres

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

Open
#930 2 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

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!

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Next.js 14 edge-runtime compilation from src/db/index.ts and follow the import trace through postgres/cf/polyfills.js and postgres/cf/src/index.js. Compare the postgres package's Cloudflare path with the reported cloudflare:sockets failure; done means the API route builds for Cloudflare Workers without the UnhandledSchemeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nextjs, postgres
Domain
backend, databases, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.