N
Neonβ€’2y ago
other-emerald

Error connection terminated unexpectedly

Hi πŸ™‚ New user of neon here. I am trying to connect to a DB from a local nextjs execution using the @neondatabase/serverless But I always get
Error: Connection terminated unexpectedly
Error: Connection terminated unexpectedly
When i connect with psql its working well. 😦 and even when im connecting with pg but without the nextjs. full stack is: NextJS14 & Kysely Would love to know what I am doing wrong.
8 Replies
xenogeneic-maroon
xenogeneic-maroonβ€’2y ago
Hey, can you try running something simple like this script?
// file: index.ts
import { neon } from '@neondatabase/serverless'

const sql = neon(process.env.DATABASE_URL!)

async function main () {
const result = await sql`SELECT version();`

console.log(result)
}

main()
// file: index.ts
import { neon } from '@neondatabase/serverless'

const sql = neon(process.env.DATABASE_URL!)

async function main () {
const result = await sql`SELECT version();`

console.log(result)
}

main()
I run it using:
DATABASE_URL='url-goes-here' npx tsx index.ts
DATABASE_URL='url-goes-here' npx tsx index.ts
Does that work for you? IF so, then can you share more details? For example, the driver version and if you're using an ORM
other-emerald
other-emeraldOPβ€’2y ago
this is working perfectly πŸ™‚ As mentioned above I am using Kysely. "kysely": "^0.27.2", "@neondatabase/serverless": "^0.7.2",
import { Kysely, PostgresDialect, sql } from "kysely";
import { Database } from "./schema";
import { Pool } from "@neondatabase/serverless";

export const adminDB = new Kysely<Database>({
dialect: new PostgresDialect({
pool: new Pool({ connectionString: process.env.DATABASE_URL! }),
}),
});

async function main() {
const result = await sql`SELECT version();`.execute(adminDB);

console.log(result);
}

main();
import { Kysely, PostgresDialect, sql } from "kysely";
import { Database } from "./schema";
import { Pool } from "@neondatabase/serverless";

export const adminDB = new Kysely<Database>({
dialect: new PostgresDialect({
pool: new Pool({ connectionString: process.env.DATABASE_URL! }),
}),
});

async function main() {
const result = await sql`SELECT version();`.execute(adminDB);

console.log(result);
}

main();
When I did the exact same thing but with kysely as you can see above I got the error:
Error: All attempts to open a WebSocket to connect to the database failed. Please refer to https://github.com/neondatabase/serverless/blob/main/CONFIG.md#websocketconstructor-typeof-websocket--undefined. Details: fetch failed
Error: All attempts to open a WebSocket to connect to the database failed. Please refer to https://github.com/neondatabase/serverless/blob/main/CONFIG.md#websocketconstructor-typeof-websocket--undefined. Details: fetch failed
other-emerald
other-emeraldOPβ€’2y ago
GitHub
neon-vercel-kysely/api/sites.ts at main Β· neondatabase/neon-vercel-...
Example use of Neon serverless driver on Vercel Edge Functions with Kysely and kysely-codegen - neondatabase/neon-vercel-kysely
other-emerald
other-emeraldOPβ€’2y ago
When I do it with the ws it works:
import { Kysely, PostgresDialect, sql } from "kysely";
import { Database } from "./schema";
import { neonConfig, Pool } from "@neondatabase/serverless";
import ws from "ws";
neonConfig.webSocketConstructor = ws;

const pool = new Pool({ connectionString: process.env.DATABASE_URL! });
export const adminDB = new Kysely<Database>({
dialect: new PostgresDialect({
pool,
}),
});

async function main() {
const result = await sql`SELECT version();`.execute(adminDB);

console.log(result);
}

main();
import { Kysely, PostgresDialect, sql } from "kysely";
import { Database } from "./schema";
import { neonConfig, Pool } from "@neondatabase/serverless";
import ws from "ws";
neonConfig.webSocketConstructor = ws;

const pool = new Pool({ connectionString: process.env.DATABASE_URL! });
export const adminDB = new Kysely<Database>({
dialect: new PostgresDialect({
pool,
}),
});

async function main() {
const result = await sql`SELECT version();`.execute(adminDB);

console.log(result);
}

main();
but then when I run it with NextJS it does not work:
TypeError: bufferUtil.mask is not a function
at module.exports.mask (webpack-internal:///(rsc)/../../node_modules/.pnpm/ws@8.16.0/node_modules/ws/lib/buffer-util.js:96:29)
TypeError: bufferUtil.mask is not a function
at module.exports.mask (webpack-internal:///(rsc)/../../node_modules/.pnpm/ws@8.16.0/node_modules/ws/lib/buffer-util.js:96:29)
xenogeneic-maroon
xenogeneic-maroonβ€’2y ago
Can you try this: https://discord.com/channels/1176467419317940276/1176467419938701375/1196916871367229570 A user reported this same error in the past
other-emerald
other-emeraldOPβ€’2y ago
wow this is working super weird :S But thanks!
xenogeneic-maroon
xenogeneic-maroonβ€’2y ago
You're welcome, glad it got you unblocked. Do you think this is something Kysely need to add in their documentation? Or maybe it's a Next.js bundling iss\ue?
other-emerald
other-emeraldOPβ€’2y ago
hmm i have no idea :S seems more like its a next bundling issue

Did you find this page helpful?