cloudflare plugin #50177
Replies: 29 comments 68 replies
-
This is a known issue with
|
Beta Was this translation helpful? Give feedback.
-
Doing exactly the same thing as @kennyyang2015 (azimuttapp/azimutt#215) ^^ |
Beta Was this translation helpful? Give feedback.
-
I am getting the same error using import { Pool } from "@neondatabase/serverless"; const pool = new Pool({ connectionString }); anyone know a fix ? |
Beta Was this translation helpful? Give feedback.
-
I fixed that issue with the following webpack config:
|
Beta Was this translation helpful? Give feedback.
-
since this is an optional dependency that we don't use, we simply delete it via postinstall: {
"scripts": {
"postinstall": "shx rm -rf node_modules/pg-cloudflare"
}
} |
Beta Was this translation helpful? Give feedback.
-
In case anyone else comes across this thread. One option, at least in my case, is to recognize this happens when using the edge runtime with postgres, so not using edge fixed it for me |
Beta Was this translation helpful? Give feedback.
-
I ran into this as well, I have a working repo here with Next.js + Drizzle + Postgres + NextAuth: https://github.com/vercel/nextjs-postgres-auth-starter |
Beta Was this translation helpful? Give feedback.
-
I encountered as well with Next.js + Drizzle + Postgres + NextAuth. |
Beta Was this translation helpful? Give feedback.
-
i tried using neon and it worked nicely. Postgres is not compatible with edge runtime is the problem. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
I just downgraded postgres version from 3.4.3 to 3.3 and it just works with DrizzleAdapter. Below are the package versions that I am using: "next": "14.1.0", |
Beta Was this translation helpful? Give feedback.
-
Currently non of these fixes are working for me. The latest suggestion, downgrading postgres to 3.4, also did not work. I will move away from middleware authentication for now. Hope to see an update on this issue at some point soon. |
Beta Was this translation helpful? Give feedback.
-
i forgot to remove these three line of code when i copy schema from auth.js : // const connectionString = "postgres://postgres:postgres@localhost:5432/drizzle"; // export const db = drizzle(pool); this is my mistake , check if you remove this lines from schema |
Beta Was this translation helpful? Give feedback.
-
Hello, we added support to be able to use the In your webpack: (config) => {
config.externals.push('cloudflare:sockets');
config.externalsType = 'commonjs';
}) |
Beta Was this translation helpful? Give feedback.
-
Had same issue with next, next-auth , drizzle and postgres (local docker) setup. All latest version of dependencies and postgres": "3.3" worked for me. Webpack fix didn't worked . "drizzle-orm": "^0.29.5", |
Beta Was this translation helpful? Give feedback.
-
I encounter this error too with lucia auth Here is my middleware file import {
DEFAULT_LOGIN_REDIRECT,
PUBLIC_ROUTES,
AUTH_ROUTES,
API_AUTH_PREFIX,
LOGIN_REDIRECT,
} from "@/routes";
import { NextRequest } from "next/server";
import { validateRequest } from "@/lib/lucia";
export async function middleware(req: NextRequest) {
const { user } = await validateRequest();
const isLogin = true;
const { nextUrl } = req;
const isApiAuthRoute = nextUrl.pathname.startsWith(API_AUTH_PREFIX);
const isAuthRoute = AUTH_ROUTES.includes(req.nextUrl.pathname);
const isPublicRoute = PUBLIC_ROUTES.includes(req.nextUrl.pathname);
if (isApiAuthRoute) {
return;
}
if (isAuthRoute) {
if (isLogin) {
return Response.redirect(new URL(DEFAULT_LOGIN_REDIRECT, nextUrl));
}
return;
}
if (!isLogin && !isPublicRoute) {
return Response.redirect(new URL(LOGIN_REDIRECT, nextUrl));
}
return;
}
export default middleware;
export const config = {
matcher: ["/((?!.+\\.[\\w]+$|_next).*)", "/", "/(api|trpc)(.*)"],
}; Here is my validate validateRequest() export const validateRequest = cache(
async (): Promise<
{ user: User; session: Session } | { user: null; session: null }
> => {
const sessionId = cookies().get(lucia.sessionCookieName)?.value ?? null;
if (!sessionId) {
return {
user: null,
session: null,
};
}
const result = await lucia.validateSession(sessionId);
// next.js throws when you attempt to set cookie when rendering page
try {
if (result.session && result.session.fresh) {
const sessionCookie = lucia.createSessionCookie(result.session.id);
cookies().set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes
);
}
if (!result.session) {
const sessionCookie = lucia.createBlankSessionCookie();
cookies().set(
sessionCookie.name,
sessionCookie.value,
sessionCookie.attributes
);
}
} catch {}
return result;
}
); Whenever I use this
|
Beta Was this translation helpful? Give feedback.
-
If anyone is still having this issue, you might have |
Beta Was this translation helpful? Give feedback.
-
If anyone is facing this issue with Supabase, try using import { Pool } from '@neondatabase/serverless'
import { drizzle } from 'drizzle-orm/neon-serverless'
const connectionString = process.env.DATABASE_URL!
const pool = new Pool({ connectionString })
export const db = drizzle(pool) |
Beta Was this translation helpful? Give feedback.
-
Who has trouble with Auth.js there is your solution: |
Beta Was this translation helpful? Give feedback.
-
I also get the same error, using local PostgreSQL: import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres"; This happened after I installed Auth.js Drizzle Adapter. Anyone have any ideas going forward? |
Beta Was this translation helpful? Give feedback.
-
this error occur while using edge run time with postgress and supabase
|
Beta Was this translation helpful? Give feedback.
-
For anyone coming here with the following configuration...
...here are the magic incantations to resolve these errors: // ./lib/db/index.ts
import { neonConfig, Pool } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-serverless';
import * as schema from './schema';
neonConfig.pipelineConnect = false;
const connectionString = process.env.DATABASE_URL!;
const pool = new Pool({ connectionString });
const db = drizzle(pool, { schema });
export default db; // ./package.json (the relevant bits)
{
"dependencies": {
"@cloudflare/next-on-pages": "^1.8.0",
"@neondatabase/serverless": "^0.9.4",
"@supabase/ssr": "^0.4.0",
"@supabase/supabase-js": "^2.44.4",
"drizzle-orm": "^0.31.2",
"supabase": "^1.183.5"
},
"devDependencies": {
"drizzle-kit": "^0.22.8"
}
} References:
UPDATEThe above ^ configuration still suffers from yet another type of error:
Unfortunately, when updating the // ./lib/db/index.ts
import { neonConfig, Pool } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-serverless';
import * as schema from './schema';
neonConfig.pipelineConnect = false;
const connectionString = process.env.DATABASE_URL!;
- const pool = new Pool({ connectionString });
+ const pool = new Pool({ connectionString, allowExitOnIdle: true });
const db = drizzle(pool, { schema });
export default db; ...another error resulted:
So, I've had to resort to exporting the // ./lib/db/index.ts
/* eslint-disable import/prefer-default-export */
import { neonConfig, Pool } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-serverless';
import * as schema from './schema';
neonConfig.pipelineConnect = false;
const connectionString = process.env.DATABASE_URL!;
- const pool = new Pool({ connectionString });
+ export const pool = new Pool({ connectionString });
- const db = drizzle(pool, { schema });
+ const db = drizzle(pool, { schema });
- export default db; // ./app/api/rows/[id]/route.ts (pseudo code)
/* eslint-disable import/prefer-default-export */
import { eq } from 'drizzle-orm';
import { NextRequest, NextResponse } from 'next/server';
import { REVALIDATE } from '~/constants';
- import db from '~/lib/db';
+ import { db, pool } from '~/lib/db';
import * as schema from '~/lib/db/schema';
import ErrorWithStatus from '~/utils/ErrorWithStatus';
import { type SanitizedRowWithOtherRows } from './types';
export const runtime = 'edge';
export const revalidate = REVALIDATE;
type Context = {
params: {
id: string;
};
};
export const GET = async (request: NextRequest, context: Context) => {
const { id } = context.params;
try {
const [row] = await db.select().from(schema.rows).where(eq(schema.rows.id, id)).limit(1);
if (!row) throw new ErrorWithStatus(`Row with ID "${id}" not found`, 404);
const otherRows = await db.select().from(schema.otherRows).where(eq(schema.otherRows.rowId, row.id));
if (!otherRows || otherRows.length === 0) throw new ErrorWithStatus(`Other rows for row ID "${row.id}" not found`, 404);
+ await pool.end();
const data: SanitizedRowWithOtherRows = {
id: row.id,
otherRows: otherRows.map((otherRow) => ({
id: otherRow.id,
rowId: otherRow.rowId,
value: otherRow.value,
})),
};
return NextResponse.json(data);
} catch (error) {
+ await pool.end();
const { message, status } = error as ErrorWithStatus;
return new NextResponse(message || 'An unexpected error occurred', { status: status || 500 });
}
}; References: |
Beta Was this translation helpful? Give feedback.
-
having similar issue when accessing session in middleware; // src/middleware.ts
import { type NextRequest, NextResponse } from "next/server";
import { auth } from "@/src/lib/auth";
export async function middleware(req: NextRequest) {
const session = await auth(); with "drizzle-kit": "~0.23.0",
"drizzle-orm": "~0.32.1",
"next": "14.2.5",
"next-auth": "^5.0.0-beta.19",
"nodemailer": "^6.9.14",
"postgres": "~3.4.4", |
Beta Was this translation helpful? Give feedback.
-
For anyone still looking for a solution for local development or any Postgres deployment that is not natively supported by the Local docker compose deployment: services:
db:
image: postgres:16.3
restart: always
shm_size: 128mb
ports:
- 5432:5432
environment:
POSTGRES_USER: root
POSTGRES_PASSWORD: root
POSTGRES_DB: dbname
POSTGRES_HOST_AUTH_METHOD: md5
db-ws-proxy:
image: ghcr.io/neondatabase/wsproxy:latest
restart: always
platform: linux/amd64
ports:
- 3100:6543
environment:
ALLOW_ADDR_REGEX: ""
LISTEN_PORT: :6543
import { neonConfig, Pool } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-serverless";
export const schema = {
// schema
};
neonConfig.wsProxy = "localhost:3100/v1";
neonConfig.useSecureWebSocket = false;
neonConfig.pipelineConnect = false; // can be removed if setting the POSTGRES_HOST_AUTH_METHOD on the postgres container to `trust`
export const db = drizzle(
new Pool({
connectionString: process.env.WS_DATABASE_URL,
}),
{ schema },
); In this example This fixes it for edge runtime code, however it will not work with drizzle-kit, as it looks like it does not load the global settings set on the The solution for drizzle-kit is just to install either Example config:
import type { Config } from "drizzle-kit";
export default {
schema: [],
out: "./migrations",
dialect: "postgresql",
dbCredentials: {
url: process.env.DATABASE_URL!,
},
} satisfies Config; Here the |
Beta Was this translation helpful? Give feedback.
-
What works for me is shifting to Neon.DB as it supports edge. Neon uses an API layer in front of their postgres database that helps edge to query db through simple db/index.ts import { Pool } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-serverless";
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
export const db = drizzle(pool); auth.ts import NextAuth from "next-auth";
import Google from "next-auth/providers/google";
import { db } from "@/db";
import { users } from "@/db/schema";
export const { handlers, signIn, signOut, auth } = NextAuth({
providers: [Google],
callbacks: {
async signIn({ user, account, profile }) {
if (account?.provider === "google") {
try {
await db
.insert(users)
.values({
email: user.email as string,
name: user.name,
image: user.image,
})
.onConflictDoUpdate({
target: users.email,
set: {
name: user.name,
image: user.image,
},
});
return true;
} catch (error) {
console.error("Error inserting user:", error);
return false;
}
}
return true;
},
},
}); |
Beta Was this translation helpful? Give feedback.
-
I am having the same issue |
Beta Was this translation helpful? Give feedback.
-
I want to know the actual reason that why this error is coming. What I am doing is that my project is built on Node.js environment and I am creating the .exe. While creating the .exe , I am getting the same error ERROR in cloudflare:sockets |
Beta Was this translation helpful? Give feedback.
-
This solution work for me. "peerDependencies": {
"pg-cloudflare": "1.1.1"
}, |
Beta Was this translation helpful? Give feedback.
-
Summary
Hi, I am a new developer trying to create a tool to visualize sql data tables, and to render and visualize the tables, I need a PostgreSQL URI so that i can connect to that database, but i am recieving this Error
" 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 "
have you guys ever received an error like this before and if so how did you solve it??
thanks in advance !
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions