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: Unable to switch from drizzle-orm/vercel-postgres to drizzle-orm/node-postgres using nodejs runtime #1271

Open
LinnJS opened this issue Dec 12, 2024 · 1 comment

Comments

@LinnJS
Copy link

LinnJS commented Dec 12, 2024

Provide environment information

System:
  OS: macOS 15.1.1
  CPU: (10) arm64 Apple M1 Max
  Memory: 8.57 GB / 64.00 GB
  Shell: 5.9 - /bin/zsh
Binaries:
  Node: 20.18.1 - ~/.nvm/versions/node/v20.18.1/bin/node
  npm: 10.8.2 - ~/.nvm/versions/node/v20.18.1/bin/npm
  pnpm: 9.14.2 - ~/.nvm/versions/node/v20.18.1/bin/pnpm
  Watchman: 2024.12.02.00 - /opt/homebrew/bin/watchman

TL;DR

I've hit a wall trying to switch the Postgres driver in the create-t3-turbo template from drizzle-orm/vercel-postgres to drizzle-orm/node-postgres. While database migrations are working fine, the app keeps failing during startup and hydration in different Node.js environments. I'm seeing net.Socket errors and Edge runtime issues with the Node.js crypto module. I've tried several workarounds, but nothing's sticking.

Describe the bug

I've run into some frustrating runtime compatibility issues while trying to swap out the Postgres driver in the create-t3-turbo template. Here's what I'm seeing:

  • On Node v22.12.0, I'm getting hit with net.Socket constructor errors in the Next.js React Server Components (RSC) environment
  • On Node v20.18.1, I'm running into Edge runtime issues with the Node.js crypto module and missing pg-native dependencies

The weird part? Database operations like schema migrations using drizzle-kit push work perfectly fine. It's just when the app tries to start up that everything falls apart.

Link to reproduction

LinnJS/t3-turborepo-node-pg-driver#1

To reproduce

  1. Clone the my reproduction repository .
  2. Pull origin git fetch origin main
  3. Switch branch git checkout feat/add-pg-driver
  4. Install pnpm install --filter @acme/nextjs...
  5. Run http://localhost:3000 pnpm dev --filter @acme/nextjs...

Previous Attempted Workarounds

I've thrown quite a few potential solutions at this problem:

  1. Removed all "Edge" exports from the project
  2. Tweaked the Webpack config in next.config.js to exclude problematic Node.js modules
  3. Verified that CLI database operations work (they do!)
  4. Even got it working in a closed-source project by suppressing similar errors, but can't replicate that success in a clean setup

Despite all these attempts, the issue keeps showing up during runtime when using the Node.js runtime.

Additional information

Introduction

Hey there! I've been diving deep into the create-t3-turbo template and hit some pretty significant roadblocks while trying to switch database drivers. My goal was to use the node-postgres driver for better compatibility with self-hosted setups, but I'm running into walls with both RSC and Edge runtime environments.

Description

The problems started when I tried switching from drizzle-orm/vercel-postgres to drizzle-orm/node-postgres in the packages/db and packages/api directories. I'm seeing runtime errors related to Node.js native modules like net and crypto. It seems to be tied to Next.js runtime limitations.

Error Messages

Original Repository (Node v22.12.0)
TypeError: net.Socket is not a constructor
    at getStream (webpack-internal:///(rsc)/../../node_modules/pg/lib/stream.js:23:12)
    at new Connection (webpack-internal:///(rsc)/../../node_modules/pg/lib/connection.js:18:36)
    at new Client (webpack-internal:///(rsc)/../../node_modules/pg/lib/client.js:48:7)
Reproduction Repository (Node v20.18.1)
Error: The edge runtime does not support Node.js 'crypto' module.
Learn More: https://nextjs.org/docs/messages/node-module-in-edge-runtime
    at <unknown> (webpack-internal:///(middleware)/../../node_modules/next/dist/esm/server/web/globals.js:34)

And just to make things more interesting, I'm also seeing this in the reproduction:

Module not found: Can't resolve 'pg-native' in '/Users/.../node_modules/pg/lib/native'

Import trace for requested module:
../../node_modules/pg/lib/native/client.js
../../node_modules/pg/lib/native/index.js
../../node_modules/pg/lib/index.js
../../packages/db/src/client.ts
../../packages/auth/src/config.ts
../../packages/auth/src/index.ts

Reproduction Repository

t3-turborepo-node-pg-driver

Related Discussions

Discord Thread Reference

If you want to join the discussion, I've started a thread in Theo's Typesafe Cult

Additional Context

From what I can tell, these errors are popping up because of how Next.js interacts with Node.js native modules in RSC and Edge runtime environments. This feels more like a fundamental runtime compatibility issue rather than just a configuration problem.

Expected Behavior

In an ideal world, the app should just connect to Postgres using the node-postgres driver without throwing fits about net.Socket, crypto, or pg-native. It should run smoothly on the Node.js runtime like any other normal app.

Questions

I'm still wrestling with these questions:

  1. Is there a "right way" to use node-postgres with Next.js RSC?
  2. Are there known limitations I should be aware of?
  3. How are others handling Node.js native modules in the Edge runtime?

Contributing Back to the Community

Once we crack this, I'd love to give back by:

  • Writing up a detailed guide on switching from vercel-postgres to node-postgres
  • Documenting all the configuration changes and workarounds needed
  • Sharing best practices for using node-postgres in Next.js RSC/Edge environments
  • Warning others about the pitfalls I've discovered

Thanks in advance for any help or insights you can share! Let's figure this out together.

@HamishBuckmaster
Copy link

We've experienced this in the past and this is because the middleware always is set to "edge" and what was done in the past is to use pg-proxy or some other setup that allows you to use something else this can be worked around if you adjust the auth config also explained here: https://authjs.dev/guides/edge-compatibility#middleware

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants