postgres icon indicating copy to clipboard operation
postgres copied to clipboard

UnhandledSchemeError when using Drizzle ORM with Next.js authentication application

Open KennyMwendwaX opened this issue 1 year ago • 7 comments

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

KennyMwendwaX avatar Feb 17 '24 05:02 KennyMwendwaX

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.

itsjavi avatar Mar 25 '24 11:03 itsjavi

same here. i was hoping it supports edge runtime

da1z avatar Apr 12 '24 16:04 da1z

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.

jckw avatar Apr 17 '24 19:04 jckw

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)

mlntr avatar May 19 '24 23:05 mlntr

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

Filip4130 avatar May 22 '24 21:05 Filip4130

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

KennyMwendwaX avatar May 29 '24 10:05 KennyMwendwaX

The solution in the video starts at 2:18:49 https://www.youtube.com/watch?v=1MTyCvS05V4&t=8327s

borisyankov avatar Jul 03 '24 03:07 borisyankov