Skip to content

bug: Unable to switch from drizzle-orm/vercel-postgres to drizzle-orm/node-postgres using nodejs runtime #1271

Open
@LinnJS

Description

@LinnJS

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions