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]: node-postgres broken with ESM #505

Closed
ksmithut opened this issue Apr 23, 2023 · 7 comments · Fixed by #520
Closed

[BUG]: node-postgres broken with ESM #505

ksmithut opened this issue Apr 23, 2023 · 7 comments · Fixed by #520
Assignees
Labels
bug Something isn't working

Comments

@ksmithut
Copy link

What version of drizzle-orm are you using?

0.25.1

What version of drizzle-kit are you using?

0.17.5

Describe the Bug

import { drizzle } from 'drizzle-orm/node-postgres'
import pg from 'pg'

const pool = new pg.Pool({
  connectionString: 'postgres://postgres:postgres@127.0.0.1:5432/postgres'
})
const db = drizzle(pool)

The above when run produces the stack trace:

TypeError: Cannot read properties of undefined (reading 'setTypeParser')
    at m.initMappers ([redacted]/node_modules/drizzle-orm/node-postgres/index.mjs:1:2315)

It's attempting to read .setTypeParser from the pg module. I've traced it down to the lines that import * as pg from 'pg. Because pg is commonjs and seems to only do module.exports = from its main module, the imported t looks like { default: mainmodule } instead of mainModule. If you change those to import pg from 'pg' that should fix it, or wherever you reference pg.setTypeParser() and any other pg properties, you just add pg.default.. I'll submit a PR when I get some free time later today.

Expected behavior

No response

Environment & setup

I've run across the same error with node 18.16.0, 19.9.0 and 20.0.0.

@ksmithut ksmithut added the bug Something isn't working label Apr 23, 2023
@dankochetov
Copy link
Contributor

If your project is ESM, do import * as pg from 'pg'

@dankochetov
Copy link
Contributor

OK, I see what the issue is. Weird, because it worked when I tested with raw Node in ESM mode.

@dankochetov
Copy link
Contributor

The thing is, I don't think we could simply do pg.default, because it won't work in CJS mode.

@ksmithut
Copy link
Author

Apologies, I forgot to include the version of pg I was using: 8.10.0. Would you like me to submit a PR? Or do you have an idea of the fix you'd like?

@gijsmin
Copy link

gijsmin commented Apr 24, 2023

Also not working for me. No way to setup Drizzle ORM now.

@Droutin
Copy link

Droutin commented Apr 24, 2023

Bewest version of drizzle-orm isnt working.. I have to downgrade to ^0.24.2

@dankochetov dankochetov self-assigned this Apr 25, 2023
@dankochetov
Copy link
Contributor

Please check drizzle-orm@beta, should be fixed there
@ksmithut @GM33 @Droutin

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

Successfully merging a pull request may close this issue.

4 participants