Open l4j3b opened 5 months ago
Have this same issue with ‘postgres’
yeah at present my dev workflow is restarting npm run preview
ad nauseum. It doesn't not make for a good DX - if anyone has done the local cloudflare build with the vite watch/hmr, please share here. following this issue to learn more ... https://github.com/cloudflare/workers-sdk/issues/5315
remix/vite/drizzle/postgres/cloudflare-pages
Ran into the same bug and got it solved by replacing drizzle-orm/postgres-js
for drizzle-orm/node-postgres
. So far, only tested on my dev env, hope it also works in the edge.
Ran into the same bug and got it solved by replacing
drizzle-orm/postgres-js
fordrizzle-orm/node-postgres
. So far, only tested on my dev env, hope it also works in the edge.
Are you able to provide an example?
I've found the root cause of the issue and a short-term workaround. I'm not certain where the fix should be applied, so I'll leave some context and allow the subject matter experts to figure out the rest. 🙂
The issue arises from how postgres.js applies the Cloudflare polyfills using package export conditionals. The runtime is being detected as a "worker," which is suitable for deployment to CF but problematic when developing locally with Remix since we're not actually running within the workerd runtime unless you use workers pages dev.
The workaround I used was to alias the import in development mode to use the default entry point instead of the polyfilled one. Might not be the best, but works for now.
// vite.config.ts
export default defineConfig(({ mode }) => ({
resolve: {
alias: {
...(mode === 'development' && { 'postgres': path.resolve(__dirname, 'node_modules/postgres/src/index.js') })
}
},
// ...
}));
@ryanflorence is there any chance this could be looked at or patched?
I've found the root cause of the issue and a short-term workaround. I'm not certain where the fix should be applied, so I'll leave some context and allow the subject matter experts to figure out the rest. 🙂
The issue arises from how postgres.js applies the Cloudflare polyfills using package export conditionals. The runtime is being detected as a "worker," which is suitable for deployment to CF but problematic when developing locally with Remix since we're not actually running within the workerd runtime unless you use workers pages dev.
The workaround I used was to alias the import in development mode to use the default entry point instead of the polyfilled one. Might not be the best, but works for now.
// vite.config.ts export default defineConfig(({ mode }) => ({ resolve: { alias: { ...(mode === 'development' && { 'postgres': path.resolve(__dirname, 'node_modules/postgres/src/index.js') }) } }, // ... }));
This is crazy, but it works!
I wanted to add my 2 cents. I have a monorepo with turborepo, and I had to find the location of postgres outside of the remix app.
import {
vitePlugin as remix,
cloudflareDevProxyVitePlugin as remixCloudflareDevProxy,
} from '@remix-run/dev';
import { defineConfig } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
import { getLoadContext } from './load-context';
import path from 'path';
export default defineConfig(({ mode }) => ({
plugins: [
remixCloudflareDevProxy({ getLoadContext }),
remix(),
tsconfigPaths(),
],
resolve: {
alias: {
...(mode === 'development' && {
postgres: path.resolve(
__dirname,
'../../libs/db/node_modules/postgres/src/index.js',
),
}),
},
},
}));
Reproduction
Hi,
I am experiencing with my setup in dev mode (it works if I build and serve my project) with my Remix app.
I am using Remix/Vite/Drizzle/Cloudflare Pages/Supabase.
The issue seems to be cause by the
await db.query.users.findMany()
in my loader:My vite config:
The error I am getting:
My scripts:
As stated above, if I do npm run build && npm run start, it works. The issue happens with npm run dev.
I have tried quite a few things:
I think it may be an issue with remix/vite/postgres? I spent quite a bit of time looking into it but have not been able to able to debug it. I have limited time and was wondering if someone else ran into that issue.
I tried with
drizzle-orm/node-postgres
. It works locally but not in prod as pg is using Node.So I guess I can use pg in "dev" and "postgres" for the build for now but I would prefer not to.
System Info
Used Package Manager
npm
Expected Behavior
It should work locally.
Actual Behavior
It is not working locally and throwing an error.