Skip to content

UnhandledSchemeError when using Drizzle ORM with Next.js authentication application #810

Closed

Description

@knnedy

Description:
I am encountering an issue while using postgres.js with Drizzle ORM in a Next.js authentication application. Despite not using Cloudflare Workers in my project, I'm encountering an error related to "cloudflare:sockets", indicating an UnhandledSchemeError.

Here's a summary of the relevant code and context:

// db.ts
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

const client = postgres(process.env.DATABASE_URL!);

const db = drizzle(client, { schema });

export default db;
// auth.ts
import NextAuth, { DefaultSession } from "next-auth";
import { authConfig } from "./auth.config";
import { DrizzleAdapter } from "@auth/drizzle-adapter";
import db from "@/database/db";

export const {
  handlers: { GET, POST },
  auth,
  signIn,
  signOut,
} = NextAuth({
  callbacks: {
    async session({ token, session }) {
      // Session callback logic
    },
    async jwt({ token }) {
      // JWT callback logic
      // Issue seems to arise here when interacting with the database
    },
  },
  adapter: DrizzleAdapter(db),
  secret: process.env.NEXTAUTH_SECRET,
  session: { strategy: "jwt" },
});
// middleware.ts
export {auth as default} from "./auth"

The error message received is:

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/postgres/cf/polyfills.js
./node_modules/postgres/cf/src/index.js
./src/database/db.ts
./src/auth.ts

Activity

  1. itsjavi commented on Mar 25, 2024

    @itsjavi

    Same here, tried with both node v18 and v20. I am on latest Next.js 14.1 and Drizzle 0.30.4

    I think it's because the edge runtime is not supported. I had export runtime = 'edge' in one of my routes using the DB. After removing it it worked.

    middleware.ts is always using edge.

  2. da1z commented on Apr 12, 2024

    @da1z

    same here. i was hoping it supports edge runtime

  3. jckw commented on Apr 17, 2024

    @jckw

    I just contributed to the bounty on this issue.

    Each contribution to this bounty has an expiry time and will be auto-refunded to the contributor if the issue is not solved before then.

    Current bounty reward

    To make this a public bounty or have a reward split, the maintainer can reply to this comment.

  4. mlntr commented on May 19, 2024

    @mlntr

    I have a similar problem in my middleware.ts. My error message is:

    [auth][error] AdapterError: Read more at [https://errors.authjs.dev#adaptererror](https://errors.authjs.dev/#adaptererror) 
    [auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
    Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime 
        at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
        at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
        at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
        at listOnTimeout (node:internal/timers:573:17)
        at process.processTimers (node:internal/timers:514:7)
    [auth][error] SessionTokenError: Read more at [https://errors.authjs.dev#sessiontokenerror](https://errors.authjs.dev/#sessiontokenerror) 
    [auth][cause]: Error: The edge runtime does not support Node.js 'net' module.
    Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime 
        at Object.apply (webpack-internal:///(middleware)/./node_modules/next/dist/esm/server/web/globals.js:43:19)
        at createSocket (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:146:44)
        at Timeout.connect [as _onTimeout] (webpack-internal:///(middleware)/./node_modules/postgres/src/connection.js:343:31)
        at listOnTimeout (node:internal/timers:573:17)
        at process.processTimers (node:internal/timers:514:7)
    
  5. Filip4130 commented on May 22, 2024

    @Filip4130
  6. knnedy commented on May 29, 2024

    @knnedy
    Author

    Just ran into this issue, found a fix. It's somwhere between 2:05:00 and 2:23:00 in https://www.youtube.com/watch?v=1MTyCvS05V4&list=WL&index=58&t=109s&ab_channel=CodeWithAntonio
    The issue that he solves in that timeframe is no auth secret, i did not see the current issue pop up because he is using the neo db drver instead of the postgres driver

  7. borisyankov commented on Jul 3, 2024

    @borisyankov
  8. added a commit that references this issue on Oct 29, 2026
    06cf284
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions