neondatabase / serverless

Connect to Neon PostgreSQL from serverless/worker/edge functions
https://www.npmjs.com/package/@neondatabase/serverless
MIT License
343 stars 13 forks source link

Error in next@13.2.0 - p.allocUnsafe is not a function #12

Closed joshma closed 1 year ago

joshma commented 1 year ago

Steps to reproduce

import { Pool } from "@neondatabase/serverless";

export default async function handler() {
  console.log(Object.keys(Pool));
  return new Response("hello");
}

export const config = {
  runtime: "edge",
};

Errors on GET with:

wait  - compiling...
warn  - ./node_modules/@neondatabase/serverless/index.js
Critical dependency: the request of a dependency is an expression

Import trace for requested module:
./node_modules/@neondatabase/serverless/index.js
./node_modules/@neondatabase/serverless/index.mjs
error - node_modules/@neondatabase/serverless/index.js (43:0) @ <unknown>
error - p.allocUnsafe is not a function
null

Expected result

Any import seems to break

Actual result

Imports don't break

Environment

next@13.2.0, node@16.14.2, @neondatabase/serverless@0.2.8

ivoilic commented 1 year ago

I also got this error with next middleware: next@13.2.4, node@19.6.0, @neondatabase/serverless@0.2.8

nivoc commented 1 year ago

+1

jawj commented 1 year ago

Thanks guys. That's interesting. I can run locally (npm run dev) without export const config = { runtime: "edge" }, and I can run on Vercel with it, but I can't run locally with it. I'll need to set aside a bit of time to have a look at why that is.

aulneau commented 1 year ago

I am also running into this 🤔 seems likely related to some Buffer usage somewhere

jawj commented 1 year ago

OK. Driver version 0.2.9 should fix the immediate issue with Buffer — but this just reveals a more serious underlying issue, which is that the Edge Runtime bundled with Next.js (and also with the Vercel CLI) doesn't yet support WebSockets for local development, even though Vercel does support them for deployed projects.

Neon and Vercel are both aware of this problem. I'll keep this issue open and keep you posted here.

ivoilic commented 1 year ago

Thanks for looking into this and for the info @jawj Is there a public Vercel/Next.js issue/PR/discussion we can all follow for progress on this on Vercel's end?

jokull commented 1 year ago

I created a minimal reproducable example on Stackblitz. I hope it's useful.

When I run npm run build I get errors related to PostgresQueryCompiler.visitNode.

This would be a really cool setup to support.

jokull commented 1 year ago

Probably an unrelated thing - kysely-neon is just not able to construct an aggregate query. Doing a simple sql\select 1`.execute(db)` yields a query but gives me:

unhandledRejection: Error: All attempts to open a WebSocket to connect to the database failed. If using Node, please install the `ws` package (or simply use the `pg` package instead).

... which I think has already been reported.

Happy to open another issue with the issue around aggregates and compiling queries.

jawj commented 1 year ago

The original issue here, which is that WebSockets aren't available with vercel dev or next dev, should now be resolved. Please shout if you're still having trouble, otherwise I'll close this issue.