Failed module resolutions using `pg` and Drizzle

Hi there, I'm using this Remix template, which works well on its own, but when I attempt to use the pg package to interface with my remote Postgres database, Miniflare is suddenly unable to resolve a number of dependencies after Remix finishes building.
✘ [ERROR] Could not resolve "events"

../../node_modules/pg-cloudflare/dist/index.js:1:29:
1 │ import { EventEmitter } from 'events';
╵ ~~~~~~~~

The package "events" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
✘ [ERROR] Could not resolve "events"

../../node_modules/pg-cloudflare/dist/index.js:1:29:
1 │ import { EventEmitter } from 'events';
╵ ~~~~~~~~

The package "events" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error.
Reproduce 1. npx create-remix@latest directory-name --template https://github.com/remix-run/remix/tree/main/templates/cloudflare-workers 2. cd directory-name 3. npm install pg drizzle-orm 4. npm install --save-dev @types/pg 4. Create an app/store.server.ts:
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

export const getDb = () => {
const pool = new Pool({ connectionString: "postgres://..." });
return drizzle(pool);
};
import { drizzle } from "drizzle-orm/node-postgres";
import { Pool } from "pg";

export const getDb = () => {
const pool = new Pool({ connectionString: "postgres://..." });
return drizzle(pool);
};
7. And import it in a route:
// app/routes/_index.tsx
import { getDb } from "~/store.server";

export const loader = async () => {
const db = getDb();
return null;
}

...
// app/routes/_index.tsx
import { getDb } from "~/store.server";

export const loader = async () => {
const db = getDb();
return null;
}

...
8. npm run dev - see above errors platform: node Naturally, I would like to not build for Node and be forced to use node_compat, but complying with the previous error anyway yields the following:
✘ [ERROR] service core:user:remix-cloudflare-workers: Uncaught Error: Dynamic require of "events" is not supported

at null.<anonymous> (index.js:6:9)
at null.<anonymous> (index.js:33583:25) in node_modules/pg/lib/client.js
at null.<anonymous> (index.js:2240:50)
at null.<anonymous> (index.js:34323:18) in node_modules/pg/lib/index.js
at null.<anonymous> (index.js:2240:50)
at null.<anonymous> (index.js:35442:26)
✘ [ERROR] service core:user:remix-cloudflare-workers: Uncaught Error: Dynamic require of "events" is not supported

at null.<anonymous> (index.js:6:9)
at null.<anonymous> (index.js:33583:25) in node_modules/pg/lib/client.js
at null.<anonymous> (index.js:2240:50)
at null.<anonymous> (index.js:34323:18) in node_modules/pg/lib/index.js
at null.<anonymous> (index.js:2240:50)
at null.<anonymous> (index.js:35442:26)
1 Reply
shay
shayOP10mo ago
I've managed to get the server to start successfully (with a warning about duplicate object keys being generated in the build, for some reason) by polyfilling the missing modules in remix.config.js:
serverNodeBuiltinsPolyfill: {
modules: {
fs: true,
events: true,
assert: true,
dns: true,
net: true,
crypto: true,
tls: true,
path: true,
stream: true,
},
globals: {
process: true,
},
},
serverNodeBuiltinsPolyfill: {
modules: {
fs: true,
events: true,
assert: true,
dns: true,
net: true,
crypto: true,
tls: true,
path: true,
stream: true,
},
globals: {
process: true,
},
},
and by enabling node_compat in wrangler.toml. So while this does allow the server to start, once the Pool is actually created, I receive the following: ✘ [ERROR] Error: Node.js net module is not supported by JSPM core outside of Node.js What can I do about this? I assume this is because of my polyfilling, can I do this without polyfilling? Resolved the issue by switching to postgres over pg. It's relatively drop-in since I'm using drizzle. Just hoping it'll work properly outside of the local environment - I was using pg because the drizzle documentation uses it in the Workers example.
Want results from more Discord servers?
Add your server