You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature request: Nitro 3 support (unwasm export condition) #1120
// routes/index.tsimport{PGlite}from'@electric-sql/pglite'import{defineHandler}from'nitro/h3'exportdefaultdefineHandler(async()=>(awaitnewPGlite().query('select 1 as one')).rows)
nitro build, node .output/server/index.mjs, then GET / answers 500 with the error above.
Describe the solution you'd like
Nitro's author suggested an unwasm export condition in PGlite's package, for proper Nitro compatibility. Nitro builds with that condition and handles .wasm imports through unwasm. Would you consider it?
@tdrz I tried both on the repro from this issue (nitro 3.0.260903-beta, Node 26.11.1).
1. Manually packaging the files (0.5.8) works, but it takes all three files, not just the data file. The Nitro way is to import the two Wasm files with ?module and load pglite.data as a server asset:
GET / returns [{"one":1}]. The downsides: the Wasm imports go through a relative node_modules path because ./dist/* isn't exported, and the pglite chunk grows from 752 kB to 23.1 MB.
2. The experimental build (electric-sql-pglite-0.5.9.tgz from #1131), with the repro unchanged, doesn't work. The pglite.data error is gone, but it now fails on the Wasm file, and the request hangs:
ENOENT: no such file or directory, open '…/.output/server/_libs/pglite.wasm'
So the data file is only part of the problem: Nitro doesn't include pglite.wasm and initdb.wasm in its output either.
The downsides: the Wasm imports go through a relative node_modules path because ./dist/* isn't exported, and the pglite chunk grows from 752 kB to 23.1 MB.
I am not sure I am understanding this completely. The PGlite package itself is around 23MB, so wouldn't it be expected to be a similar size when packaging it like that?
IMO we should just update the documentation since option 1 seems to work fine.
The PGlite package itself is around 23MB, so wouldn't it be expected to be a similar size when packaging it like that?
Not really, @tdrz, the two numbers only match by coincidence. Based on v0.5.8:
what PGlite actually needs at runtime is pglite.wasm (10.1MB), pglite.data (6.3MB), initdb.wasm (0.4MB) plus ~1MB of js, so ~17.8MB raw / ~5.6MB gzipped. The rest of the 26MB dist is sourcemaps (2.9MB), the cjs duplicate (1.1MB), types and the extension tarballs (1.5MB, only needed when the extension is actually used)
the 23.1MB chunk is those binaries base64-inlined into the JS: 13.5 + 8.4 + 0.5 + the 0.75MB chunk = 23.1. That's the Nitro node preset default (esmImport: false), and serverAssets goes through the same base64 inlining in production builds
So the manual recipe costs ~33% more than the files themselves, plus a 23MB string to parse and decode into memory at startup. Size on disk was never really the concern IMO, the inlining is.
That is basically what the unwasm condition was buying: the .wasm files get emitted next to the bundle untouched and imported natively, and the package points to them itself instead of the user reaching into node_modules/.../dist/*.wasm?module.
#1131 actually helps a lot here btw: the data lands in pglite.wasm itself (10.1 → 16.5MB, pglite.js unchanged, and emscripten mounts it as a zero-copy slice of linear memory, same as the preload path did). With it the whole problem collapses to "ship two .wasm files", which is precisely the case unwasm handles natively, and matches what @OrlS15 saw: the data error is gone, the wasm one remains.
So if the condition is a no-go, a much smaller ask that still unblocks the documented recipe: expose ./dist/*.wasm (and ./dist/pglite.data until #1131 lands) in exports, so bundlers can import them by package path instead of a relative node_modules one. Would you take a PR for that?
PS: for context, I maintain nuxt-pglite and today I just trace the whole package as external on Nitro 3, so all 26MB get copied over. Works, but it's exactly the thing I'd like to get rid of 😬
The PGlite package itself is around 23MB, so wouldn't it be expected to be a similar size when packaging it like that?
Not really, @tdrz, the two numbers only match by coincidence. Based on v0.5.8:
what PGlite actually needs at runtime is pglite.wasm (10.1MB), pglite.data (6.3MB), initdb.wasm (0.4MB) plus ~1MB of js, so ~17.8MB raw / ~5.6MB gzipped. The rest of the 26MB dist is sourcemaps (2.9MB), the cjs duplicate (1.1MB), types and the extension tarballs (1.5MB, only needed when the extension is actually used)
the 23.1MB chunk is those binaries base64-inlined into the JS: 13.5 + 8.4 + 0.5 + the 0.75MB chunk = 23.1. That's the Nitro node preset default (esmImport: false), and serverAssets goes through the same base64 inlining in production builds
So the manual recipe costs ~33% more than the files themselves, plus a 23MB string to parse and decode into memory at startup. Size on disk was never really the concern IMO, the inlining is.
That is basically what the unwasm condition was buying: the .wasm files get emitted next to the bundle untouched and imported natively, and the package points to them itself instead of the user reaching into node_modules/.../dist/*.wasm?module.
#1131 actually helps a lot here btw: the data lands in pglite.wasm itself (10.1 → 16.5MB, pglite.js unchanged, and emscripten mounts it as a zero-copy slice of linear memory, same as the preload path did). With it the whole problem collapses to "ship two .wasm files", which is precisely the case unwasm handles natively, and matches what @OrlS15 saw: the data error is gone, the wasm one remains.
So if the condition is a no-go, a much smaller ask that still unblocks the documented recipe: expose ./dist/*.wasm (and ./dist/pglite.data until #1131 lands) in exports, so bundlers can import them by package path instead of a relative node_modules one. Would you take a PR for that?
PS: for context, I maintain nuxt-pglite and today I just trace the whole package as external on Nitro 3, so all 26MB get copied over. Works, but it's exactly the thing I'd like to get rid of 😬
Was this (partly) written with AI? I find it very difficult to read.
Sorry @tdrz, it comes from my notes that I do share with my agents, so yes polluted. I'll try to elaborate it differently
Let's ignore the 23.1MB coincidence: that number is not the PGlite package size, it's what @OrlS15 got from nitro inlining the wasm and data bytes into the JS as base64 (~33% bigger than the files themselves, that's just base64)
PGlite currently locates its binaries at runtime with new URL("./pglite.wasm", import.meta.url) (same for initdb.wasm and pglite.data). That works as long as the files sit next to the JS, like in node_modules.
A bundler only copies what it can see imported, and a path built at runtime is invisible to it. So the bundled copy of PGlite ends up in the output folder alone, the new URL now points next to that new copy, and the file isn't there: that's the ENOENT from the issue.
What #1121 tried was adding a second entry that does a real import of the wasm files, served only to bundlers asking for the unwasm condition. Those bundlers then see the files and ship them themselves, while every other consumer keeps the current entry untouched
The potential workaround
Manually importing those files is indeed an option, but today it means something like import wasm from '../node_modules/@electric-sql/pglite/dist/pglite.wasm?module', since dist/ isn't exported. It does work, but breaks as soon as you try to build a downstream library (like in my case) or use a pnpm monorepo.
If PGlite's package.json had "./pglite.wasm": "./dist/pglite.wasm" in exports (same for initdb.wasm), the documented workaround could become import pgliteWasm from '@electric-sql/pglite/pglite.wasm?module' and that would be enough to unlock this. And if #1131 lands, those two wasm files become everything that is needed alongside docs
9. If PGlite's package.json had "./pglite.wasm": "./dist/pglite.wasm" in exports (same for initdb.wasm), the documented workaround could become import pgliteWasm from '@electric-sql/pglite/pglite.wasm?module' and that would be enough to unlock this. And if #1131 lands, those two wasm files become everything that is needed alongside docs
This looks reasonable, thanks! I'll get back to this asap.
11. @sandros94 Is there the possibility to import the .data file as well instead of merging #1131 ?
Yes, exporting all three assets lets downstream statically import and supply them directly through the existing options, that would enable the manual workaround.
Having the bundler handle those imports automatically was what the unwasm entry in #1121 aimed to address, allowing supporting both types of consumers simultaneously (current js neutral implementation and bundlers)
For now I believe it's better to stick to the same workaround as for esbuild:
Copy pglite.wasm, initdb.wasm and pglite.data from node_modules/@electric-sql/pglite/dist/ to your public/build directory so your web server can serve them.
Pass them manually when creating a PGlite instance
current workaround of copying is not a universal solution and limits the platform support to node with node_modules resolution requirement. we could even automate this by adding pglite to nf3 incompatibility db (unjs/nf3#75) for nitro but it is really not a good solution.
@tdrz i would really appreciate if we can find a way together to make a zero config universal solution so pglite works as a first class db driver for nitro ❤️
(we need some sort of a new explicit unwasm export condition to unblock this)
14. current workaround of copying is not a universal solution and limits the platform support to node with node_modules resolution requirement.
That is not true. Just read again the workaround:
Copy pglite.wasm, initdb.wasm and pglite.data from node_modules/@electric-sql/pglite/dist/ to your public/build directory so your web server can serve them.
You can also get those binary files from our CI, if you don't want to npm install @electric-sql/pglite in a throwaway folder. My understanding is that Nitro can package static assets, so that's all that's needed in your project.
This is a universal workaround, where dealing with those three binaries can be adapted to your bundler.
@tdrz i would really appreciate if we can find a way together to make a zero config universal solution so pglite works as a first class db driver for nitro ❤️
(we need some sort of a new explicit unwasm export condition to unblock this)
A zero config solution would be good, but I'm not convinced unwasm export condition is the way to go.
import{PGlite}from'@electric-sql/pglite'importpgliteWasmModulefrom'@electric-sql/pglite/pglite.wasm?module'importinitdbWasmModulefrom'@electric-sql/pglite/initdb.wasm?module'// pglite.data isn't Wasm, so load it as a server assetconstdata=awaituseStorage('assets:pglite').getItemRaw('pglite.data')constdb=awaitPGlite.create({ pgliteWasmModule, initdbWasmModule,fsBundle: newBlob([data])})
Activity
tdrz commentedon Oct 7, 2026
Interesting! Does Nitro v2 works?
pi0 commentedon Oct 7, 2026
@tdrz it is same requirement for all nitro versions. opening a PR shortly.
tdrz commentedon Oct 12, 2026
@OrlS15 Could you please try #1121 (comment)
orls0xF commentedon Oct 12, 2026
@tdrz I tried both on the repro from this issue (nitro 3.0.260903-beta, Node 26.11.1).
1. Manually packaging the files (0.5.8) works, but it takes all three files, not just the data file. The Nitro way is to import the two Wasm files with
?moduleand loadpglite.dataas a server asset:GET /returns[{"one":1}]. The downsides: the Wasm imports go through a relativenode_modulespath because./dist/*isn't exported, and the pglite chunk grows from 752 kB to 23.1 MB.2. The experimental build (
electric-sql-pglite-0.5.9.tgzfrom #1131), with the repro unchanged, doesn't work. Thepglite.dataerror is gone, but it now fails on the Wasm file, and the request hangs:So the data file is only part of the problem: Nitro doesn't include
pglite.wasmandinitdb.wasmin its output either.tdrz commentedon Oct 12, 2026
@OrlS15 Thank you for this!
I am not sure I am understanding this completely. The PGlite package itself is around 23MB, so wouldn't it be expected to be a similar size when packaging it like that?
IMO we should just update the documentation since option 1 seems to work fine.
sandros94 commentedon Oct 15, 2026
Not really, @tdrz, the two numbers only match by coincidence. Based on v0.5.8:
pglite.wasm(10.1MB),pglite.data(6.3MB),initdb.wasm(0.4MB) plus ~1MB of js, so ~17.8MB raw / ~5.6MB gzipped. The rest of the 26MBdistis sourcemaps (2.9MB), the cjs duplicate (1.1MB), types and the extension tarballs (1.5MB, only needed when the extension is actually used)esmImport: false), andserverAssetsgoes through the same base64 inlining in production buildsSo the manual recipe costs ~33% more than the files themselves, plus a 23MB string to parse and decode into memory at startup. Size on disk was never really the concern IMO, the inlining is.
That is basically what the
unwasmcondition was buying: the.wasmfiles get emitted next to the bundle untouched and imported natively, and the package points to them itself instead of the user reaching intonode_modules/.../dist/*.wasm?module.#1131 actually helps a lot here btw: the data lands in
pglite.wasmitself (10.1 → 16.5MB,pglite.jsunchanged, and emscripten mounts it as a zero-copy slice of linear memory, same as the preload path did). With it the whole problem collapses to "ship two.wasmfiles", which is precisely the caseunwasmhandles natively, and matches what @OrlS15 saw: the data error is gone, the wasm one remains.So if the condition is a no-go, a much smaller ask that still unblocks the documented recipe: expose
./dist/*.wasm(and./dist/pglite.datauntil #1131 lands) inexports, so bundlers can import them by package path instead of a relativenode_modulesone. Would you take a PR for that?PS: for context, I maintain nuxt-pglite and today I just trace the whole package as external on Nitro 3, so all 26MB get copied over. Works, but it's exactly the thing I'd like to get rid of 😬
EDIT: fyi @pi0, and correct me if I'm wrong
tdrz commentedon Oct 15, 2026
Was this (partly) written with AI? I find it very difficult to read.
sandros94 commentedon Oct 15, 2026
Sorry @tdrz, it comes from my notes that I do share with my agents, so yes polluted. I'll try to elaborate it differently
Let's ignore the 23.1MB coincidence: that number is not the PGlite package size, it's what @OrlS15 got from nitro inlining the wasm and data bytes into the JS as base64 (~33% bigger than the files themselves, that's just base64)
The two points I'd like to clarify are:
The unwasm export condition (what @pi0's PR did)
PGlite currently locates its binaries at runtime with
new URL("./pglite.wasm", import.meta.url)(same forinitdb.wasmandpglite.data). That works as long as the files sit next to the JS, like innode_modules.A bundler only copies what it can see imported, and a path built at runtime is invisible to it. So the bundled copy of PGlite ends up in the output folder alone, the
new URLnow points next to that new copy, and the file isn't there: that's theENOENTfrom the issue.What #1121 tried was adding a second entry that does a real
importof the wasm files, served only to bundlers asking for theunwasmcondition. Those bundlers then see the files and ship them themselves, while every other consumer keeps the current entry untouchedThe potential workaround
Manually importing those files is indeed an option, but today it means something like
import wasm from '../node_modules/@electric-sql/pglite/dist/pglite.wasm?module', sincedist/isn't exported. It does work, but breaks as soon as you try to build a downstream library (like in my case) or use a pnpm monorepo.If PGlite's
package.jsonhad"./pglite.wasm": "./dist/pglite.wasm"inexports(same forinitdb.wasm), the documented workaround could becomeimport pgliteWasm from '@electric-sql/pglite/pglite.wasm?module'and that would be enough to unlock this. And if #1131 lands, those two wasm files become everything that is needed alongside docstdrz commentedon Oct 15, 2026
This looks reasonable, thanks! I'll get back to this asap.
tdrz commentedon Oct 19, 2026
@sandros94 Is there the possibility to import the .data file as well instead of merging #1131 ?
sandros94 commentedon Oct 19, 2026
Yes, exporting all three assets lets downstream statically import and supply them directly through the existing options, that would enable the manual workaround.
Having the bundler handle those imports automatically was what the unwasm entry in #1121 aimed to address, allowing supporting both types of consumers simultaneously (current js neutral implementation and bundlers)
tdrz commentedon Oct 23, 2026
For now I believe it's better to stick to the same workaround as for
esbuild:Copy pglite.wasm, initdb.wasm and pglite.data from node_modules/@electric-sql/pglite/dist/ to your public/build directory so your web server can serve them.
Pass them manually when creating a PGlite instance
Also I see this on Nitro 3 docs website:
I understand that
databaseis still experimental though.pi0 commentedon Oct 24, 2026
current workaround of copying is not a universal solution and limits the platform support to node with
node_modulesresolution requirement. we could even automate this by adding pglite to nf3 incompatibility db (unjs/nf3#75) for nitro but it is really not a good solution.@tdrz i would really appreciate if we can find a way together to make a zero config universal solution so pglite works as a first class db driver for nitro ❤️
(we need some sort of a new explicit unwasm export condition to unblock this)
tdrz commentedon Oct 25, 2026
That is not true. Just read again the workaround:
You can also get those binary files from our CI, if you don't want to
npm install @electric-sql/pglitein a throwaway folder. My understanding is that Nitro can package static assets, so that's all that's needed in your project.This is a universal workaround, where dealing with those three binaries can be adapted to your bundler.
A zero config solution would be good, but I'm not convinced unwasm export condition is the way to go.
tdrz commentedon Oct 25, 2026
I need to try this:
{ "exports": { ".": { /* existing entry, unchanged */ }, "./pglite.wasm": "./dist/pglite.wasm", "./initdb.wasm": "./dist/initdb.wasm", "./pglite.data": "./dist/pglite.data", "./package.json": "./package.json" }, "files": ["dist"] }Then use them with Nuxt:
tdrz commentedon Oct 25, 2026
@pi0 Please have a look at this CI run https://github.com/electric-sql/pglite/actions/runs/40180347963
The package exports those 3 binaries. eg you can use the artifact https://github.com/electric-sql/pglite/actions/runs/40180347963/artifacts/12593334511
My understanding is that nitro can use those. Please let me know if that works.