electric-sql / pglite

Lightweight WASM Postgres with real-time, reactive bindings.
https://pglite.dev
Apache License 2.0
9.43k stars 203 forks source link

`Failed to resolve './' from './node\_modules/@electric-sql/pglite/dist/postgres.js'` #328

Closed ashrielbrian closed 1 month ago

ashrielbrian commented 2 months ago

I am using Plasmo to build a Chrome extension with pglite as the persistent data store running in a background worker.

Backing plasmo is, I believe, a node server running a live hot reload for each new change. However, when I try to load pglite in my background worker script, i.e.

import { PGlite } from "@electric-sql/pglite";

I get the errors:

🔴 ERROR  | Failed to resolve './' from './node\_modules/@electric-sql/pglite/dist/postgres.js'
🔴 ERROR  | Cannot load file './' in './node\_modules/@electric-sql/pglite/dist'.

Looking into postgres.js I believe the culprit is on line 318:

  scriptDirectory = require('url').fileURLToPath(new URL("./", import.meta.url)); // includes trailing slash

I removed "./":

scriptDirectory = require('url').fileURLToPath(new URL(import.meta.url)); // includes trailing slash

and it was able to find the modules correctly. This was something that only occurred specifically with pglite, and not other modules/packages I had installed.

Versions:

"@electric-sql/pglite": "0.2.7"
"plasmo": "0.89.2"
seanmorris commented 1 month ago

I think this Emscripten patch should fix it. https://github.com/emscripten-core/emscripten/pull/22605/files

pmp-p commented 1 month ago

0.2.10 should build with newer emscripten, did it fix the problem ?

seanmorris commented 1 month ago

0.2.10 should build with newer emscripten, did it fix the problem ?

Looks like it was still pre-patch. Line 321 of dist/postgres.js still shows the old notation that messes with webpack:

https://www.npmjs.com/package/@electric-sql/pglite?activeTab=code

The patch we need was introduced to Emscripten in commit 543993ecf6fbf1c97c2ed56d062dc504978962c3:

https://github.com/emscripten-core/emscripten/commit/543993ecf6fbf1c97c2ed56d062dc504978962c3

seanmorris commented 1 month ago

I'm seeing this working with Webpack and PGlite v0.2.11.