electric-sql / pglite

Lightweight Postgres packaged as WASM into a TypeScript library for the browser, Node.js, Bun and Deno
https://electric-sql.com
Apache License 2.0
4.76k stars 81 forks source link

`import("module")).default` is undefined #71

Closed jgoux closed 2 months ago

jgoux commented 3 months ago

Hello 👋,

I'm experimenting with pglite for an upcoming adapter for @snaplet/seed.

I'm not exactly sure why, but default is undefined when it hits this line: https://github.com/electric-sql/pglite/blob/a88952002b09bb06805d1fc137a6bdad00c62355/packages/pglite/src/utils.ts#L11

My setup is quite complicated as I define the PGlite() connection within a TypeScript configuration file loaded through https://github.com/unjs/c12 😅

But everything seems to work if I remove default and directly consume createRequire like this:

require = (await import("module")).createRequire(import.meta.url);

Do you think it would be possible to have a fallback so it works in both cases:

const module = await import("module")
const createRequire = module.default?.createRequire ?? module.createRequire
require = createRequire(import.meta.url);
samwillis commented 2 months ago

Yep, I'm happy to make that change. I've seen similar things before although of the top of my head I'm not sure what triggers it (I tempted to blame CJS vs EMS...)

jgoux commented 2 months ago

I tempted to blame CJS vs EMS...

It's always CJS vs ESM. 🥲

samwillis commented 2 months ago

Fixed in v0.1.2