Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG][Postgres][Next 13][API Route][Edge runtime] Can't resolve 'net' #753

Closed
atanaskanchev opened this issue Jun 12, 2023 · 8 comments
Closed
Labels
bug Something isn't working

Comments

@atanaskanchev
Copy link

What version of drizzle-orm are you using?

0.26.5

What version of drizzle-kit are you using?

0.18.1

Describe the Bug

Next 13 api route handler with edge runtime throws Module not found: Can't resolve 'net'

client

import { schema, type Schema } from '@lf/services-drizzle-orm-schema'
import { drizzle, type PostgresJsDatabase } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'

const queryClient = postgres('postgresql://postgres:postgres@localhost:54322/postgres')

export const db: PostgresJsDatabase<Schema> = drizzle(queryClient, { schema, logger: true })

api route

export const runtime = 'edge'

export async function GET() {
  const res = await db.query.contact.findMany()

  return NextResponse.json({ data: res })
}

error

@lf/web:dev: - wait compiling /api/contact/route (client and server)...
@lf/web:dev: - error ../../node_modules/postgres/src/connection.js:1:0
@lf/web:dev: Module not found: Can't resolve 'net'
@lf/web:dev: 
@lf/web:dev: https://nextjs.org/docs/messages/module-not-found
@lf/web:dev: 
@lf/web:dev: Import trace for requested module:
@lf/web:dev: ../../node_modules/postgres/src/index.js
@lf/web:dev: ../../packages/services/drizzle-orm-client/lib/client.ts
@lf/web:dev: ../../packages/services/drizzle-orm-client/lib/index.ts
@lf/web:dev: ./src/app/api/contact/route.ts
@lf/web:dev: ../../node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=%2FUsers%2Fatanas%2FDocuments%2FGitHub%2Flandlord-fusion-t3%2Fapps%2Fweb%2Fsrc%2Fapp%2Fapi%2Fcontact%2Froute.ts&page=%2Fapi%2Fcontact%2Froute&appDirLoader=bmV4dC1hcHAtbG9hZGVyP25hbWU9YXBwJTJGYXBpJTJGY29udGFjdCUyRnJvdXRlJnBhZ2U9JTJGYXBpJTJGY29udGFjdCUyRnJvdXRlJmFwcFBhdGhzPSZwYWdlUGF0aD1wcml2YXRlLW5leHQtYXBwLWRpciUyRmFwaSUyRmNvbnRhY3QlMkZyb3V0ZS50cyZhcHBEaXI9JTJGVXNlcnMlMkZhdGFuYXMlMkZEb2N1bWVudHMlMkZHaXRIdWIlMkZsYW5kbG9yZC1mdXNpb24tdDMlMkZhcHBzJTJGd2ViJTJGc3JjJTJGYXBwJnBhZ2VFeHRlbnNpb25zPXRzeCZwYWdlRXh0ZW5zaW9ucz10cyZwYWdlRXh0ZW5zaW9ucz1qc3gmcGFnZUV4dGVuc2lvbnM9anMmcm9vdERpcj0lMkZVc2VycyUyRmF0YW5hcyUyRkRvY3VtZW50cyUyRkdpdEh1YiUyRmxhbmRsb3JkLWZ1c2lvbi10MyUyRmFwcHMlMkZ3ZWImaXNEZXY9dHJ1ZSZ0c2NvbmZpZ1BhdGg9dHNjb25maWcuanNvbiZiYXNlUGF0aD0mYXNzZXRQcmVmaXg9Jm5leHRDb25maWdPdXRwdXQ9JnByZWZlcnJlZFJlZ2lvbj0h&nextConfigOutput=&preferredRegion=!

Expected behavior

Should be able to use Drizzle Postgres client in a Next 13 edge api route

Environment & setup

No response

@atanaskanchev atanaskanchev added the bug Something isn't working label Jun 12, 2023
@atanaskanchev atanaskanchev changed the title [BUG][Next 13][API Route][Edge runtime] Can't resolve 'net' [BUG][Postgres][Next 13][API Route][Edge runtime] Can't resolve 'net' Jun 12, 2023
@atanaskanchev
Copy link
Author

Tried swapping the postgres client with pg, however how I'm getting

error

@lf/web:dev: - wait compiling /api/contact/route (client and server)...
@lf/web:dev: - error ../../node_modules/pg-connection-string/index.js:72:69
@lf/web:dev: Module not found: Can't resolve 'fs'
@lf/web:dev: 
@lf/web:dev: https://nextjs.org/docs/messages/module-not-found
@lf/web:dev: 
@lf/web:dev: Import trace for requested module:
@lf/web:dev: ../../node_modules/pg/lib/connection-parameters.js
@lf/web:dev: ../../node_modules/pg/lib/client.js
@lf/web:dev: ../../node_modules/pg/lib/index.js
@lf/web:dev: ../../packages/services/drizzle-orm-client/lib/client.ts
@lf/web:dev: ../../packages/services/drizzle-orm-client/lib/index.ts
@lf/web:dev: ./src/app/api/contact/route.ts
@lf/web:dev: ../../node_modules/next/dist/build/webpack/loaders/next-edge-app-route-loader/index.js?absolutePagePath=%2FUsers%2Fatanas%2FDocuments%2FGitHub%2Flandlord-fusion-t3%2Fapps%2Fweb%2Fsrc%2Fapp%2Fapi%2Fcontact%2Froute.ts&page=%2Fapi%2Fcontact%2Froute&appDirLoader=bmV4dC1hcHAtbG9hZGVyP25hbWU9YXBwJTJGYXBpJTJGY29udGFjdCUyRnJvdXRlJnBhZ2U9JTJGYXBpJTJGY29udGFjdCUyRnJvdXRlJmFwcFBhdGhzPSZwYWdlUGF0aD1wcml2YXRlLW5leHQtYXBwLWRpciUyRmFwaSUyRmNvbnRhY3QlMkZyb3V0ZS50cyZhcHBEaXI9JTJGVXNlcnMlMkZhdGFuYXMlMkZEb2N1bWVudHMlMkZHaXRIdWIlMkZsYW5kbG9yZC1mdXNpb24tdDMlMkZhcHBzJTJGd2ViJTJGc3JjJTJGYXBwJnBhZ2VFeHRlbnNpb25zPXRzeCZwYWdlRXh0ZW5zaW9ucz10cyZwYWdlRXh0ZW5zaW9ucz1qc3gmcGFnZUV4dGVuc2lvbnM9anMmcm9vdERpcj0lMkZVc2VycyUyRmF0YW5hcyUyRkRvY3VtZW50cyUyRkdpdEh1YiUyRmxhbmRsb3JkLWZ1c2lvbi10MyUyRmFwcHMlMkZ3ZWImaXNEZXY9dHJ1ZSZ0c2NvbmZpZ1BhdGg9dHNjb25maWcuanNvbiZiYXNlUGF0aD0mYXNzZXRQcmVmaXg9Jm5leHRDb25maWdPdXRwdXQ9

client

import { schema } from '@lf/services-drizzle-orm-schema'
import { drizzle } from 'drizzle-orm/node-postgres'
import { Client } from 'pg'
import postgres from 'postgres'

const client = new Client({
  connectionString: 'postgresql://postgres:postgres@localhost:54322/postgres'
})

const queryClient = postgres('postgresql://postgres:postgres@localhost:54322/postgres')

client.connect()

// export const db: PostgresJsDatabase<Schema> = drizzle(queryClient, { schema, logger: true })
export const db = drizzle(client, { schema, logger: true })

@clifinger
Copy link

clifinger commented Jun 13, 2023

Weird, seem you run it on the client side ...

/api/contact/route (client and server) .

I am not an expert of Nextjs, but routes are in pages no ? like pages/api

@atanaskanchev
Copy link
Author

Weird, seem you run it on the client side ...

/api/contact/route (client and server) .

I am not an expert of Nextjs, but routes are in pages no ? like pages/api

The Next 13 API routes run on the server. Please note that the same endpoint works OK with export const runtime = 'nodejs' and fails with the edge runtime

I've expected it work out of the box, but it might be the case only for some specific mysql clients

Live on the edge
We support every major serverful and serverless runtime
live on the edge
Cloudflare Workers
Supabase functions
Vercel functions
Deno deploy
Bun
Lagon
Lagon
ElectronJS
Browser

@clifinger
Copy link

clifinger commented Jun 13, 2023

The Edge Runtime, which is used by Next.js Middleware, does not support Node.js native APIs.

From the Edge Runtime documentation:

The Edge Runtime has some restrictions including:

Native Node.js APIs are not supported. For example, you can't read or write to the filesystem
Node Modules can be used, as long as they implement ES Modules and do not use any native Node.js APIs
You can't use Node.js libraries that use fs in Next.js Middleware. Try using a client-side library instead.

If you use Vercel : https://vercel.com/templates/next.js/postgres-drizzle
Neon: https://neon.tech/docs/serverless/serverless-driver

@clifinger
Copy link

BTW you are on edge, so you should use serverless runtimes : @neondatabase/serverless, @vercel/postgres, @planetscale/database or D1 by cloudflare

@atanaskanchev
Copy link
Author

Thanks for he hints @clifinger! I am using Supabase Postgres, not sure if I can use it with Drizzle on the edge... I've tried adapting the @vercel/postgress client, but it won't connect

@clifinger
Copy link

Thanks for he hints @clifinger! I am using Supabase Postgres, not sure if I can use it with Drizzle on the edge... I've tried adapting the @vercel/postgress client, but it won't connect

Hello, no you can't as they don't have serverless edge driver in Supabase.
But I don't understand why you need to access to your DB (with postgres client) from Nextjs as you can already with the graphql, Rest or Realtime Api ?

For your own logic you can use Edge from supabase ...

If it's just to try Drizzle with supabase, you can install NestJS and follow my documentation : https://github.com/canduma/nest-drizzle

@Angelelz
Copy link
Collaborator

Drizzle is edge ready. You should select a driver that's also edge ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants