Skip to content

Feature request: Nitro 3 support (unwasm export condition) #1120

Open

Description

@orls0xF

New feature request
Support for Nitro 3, the server engine of Nuxt 5 and others, without extra configuration.

A Nitro 3 build bundles PGlite, and the server then fails on the first query because pglite.data is not next to the bundle:

ENOENT: no such file or directory, open '…/.output/server/_libs/pglite.data'

Repro with nitro 3.0.260903-beta, @electric-sql/pglite 0.5.8, Node 26:

// nitro.config.ts
import { defineConfig } from 'nitro'

export default defineConfig({ serverDir: './' })
// routes/index.ts
import { PGlite } from '@electric-sql/pglite'
import { defineHandler } from 'nitro/h3'

export default defineHandler(async () => (await new PGlite().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?

Activity

  1. tdrz commented on Oct 7, 2026

    @tdrz
    Contributor

    Interesting! Does Nitro v2 works?

  2. pi0 commented on Oct 7, 2026

    @pi0

    @tdrz it is same requirement for all nitro versions. opening a PR shortly.

  3. tdrz commented on Oct 12, 2026

    @tdrz
    Contributor

    @OrlS15 Could you please try #1121 (comment)

  4. orls0xF commented on Oct 12, 2026

    @orls0xF
    Author

    @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:

    // nitro.config.ts
    serverAssets: [
      { baseName: 'pglite', dir: './node_modules/@electric-sql/pglite/dist', pattern: 'pglite.data' },
    ]
    // routes/index.ts
    import pgliteWasmModule from '../node_modules/@electric-sql/pglite/dist/pglite.wasm?module'
    import initdbWasmModule from '../node_modules/@electric-sql/pglite/dist/initdb.wasm?module'
    
    const data = await useStorage('assets:pglite').getItemRaw('pglite.data')
    const db = await PGlite.create({ pgliteWasmModule, initdbWasmModule, fsBundle: new Blob([data]) })

    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.

  5. tdrz commented on Oct 12, 2026

    @tdrz
    Contributor

    @OrlS15 Thank you for this!

    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.

  6. sandros94 commented on Oct 15, 2026

    @sandros94
    Contributor

    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:

    1. 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)
    2. 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 😬

    EDIT: fyi @pi0, and correct me if I'm wrong

  7. tdrz commented on Oct 15, 2026

    @tdrz
    Contributor

    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:

    1. 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)
    2. 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 😬

    EDIT: fyi @pi0, and correct me if I'm wrong

    Was this (partly) written with AI? I find it very difficult to read.

  8. sandros94 commented on Oct 15, 2026

    @sandros94
    Contributor

    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)

    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 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. tdrz commented on Oct 15, 2026

    @tdrz
    Contributor

    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.

  10. tdrz commented on Oct 19, 2026

    @tdrz
    Contributor

    @sandros94 Is there the possibility to import the .data file as well instead of merging #1131 ?

  11. sandros94 commented on Oct 19, 2026

    @sandros94
    Contributor

    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)

  12. tdrz commented on Oct 23, 2026

    @tdrz
    Contributor

    For now I believe it's better to stick to the same workaround as for esbuild:

    1. 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.

    2. Pass them manually when creating a PGlite instance

    import { PGlite } from '@electric-sql/pglite'
    
    const [pgliteWasmModule, initdbWasmModule, fsBundle] = await Promise.all([
      WebAssembly.compileStreaming(fetch('/pglite.wasm')),
      WebAssembly.compileStreaming(fetch('/initdb.wasm')),
      fetch('/pglite.data').then((response) => response.blob()),
    ])
    
    const db = await PGlite.create({
      pgliteWasmModule,
      initdbWasmModule,
      fsBundle,
    })
    

    Also I see this on Nitro 3 docs website:

    Image

    I understand that database is still experimental though.

  13. pi0 commented on Oct 24, 2026

    @pi0

    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. tdrz commented on Oct 25, 2026

    @tdrz
    Contributor

    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.

  15. tdrz commented on Oct 25, 2026

    @tdrz
    Contributor

    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:

    import { PGlite } from '@electric-sql/pglite'
    import pgliteWasmModule from '@electric-sql/pglite/pglite.wasm?module'
    import initdbWasmModule from '@electric-sql/pglite/initdb.wasm?module'
    
    // pglite.data isn't Wasm, so load it as a server asset
    const data = await useStorage('assets:pglite').getItemRaw('pglite.data')
    
    const db = await PGlite.create({ pgliteWasmModule, initdbWasmModule, fsBundle: new Blob([data]) })
  16. tdrz commented on Oct 25, 2026

    @tdrz
    Contributor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions