Build fails in Bundling of edge function failed - error: Uncaught (in promise) Error: The module's source code could not be parsed: Unexpected character '\u{7f}' at file:///root/.netlify/edge-functions/server/server.js:2:4

Hello all,

Recently, builds started failing in Netlify with the following in the logs…

ℹ Building Nitro Server (preset: netlify-edge)                                                                                                                                                               nitro 1:48:17 AM
✔ Nitro server built                                                                                                                                                                                         nitro 1:49:18 AM
  └─ .netlify/edge-functions/server/server.js (2.85 MB) (788 kB gzip)
Σ Total size: 2.85 MB (788 kB gzip)
  ├─ /__sitemap__/routes.json (0ms)                                                                                                                                                                           nitro 1:49:18 AM

(build.command completed in 1m 24s)

Edge Functions bundling                                       
────────────────────────────────────────────────────────────────

Packaging Edge Functions from .netlify/edge-functions directory:
 - server
error: Uncaught (in promise) Error: The module's source code could not be parsed: Unexpected character '\u{7f}' at file:///root/.netlify/edge-functions/server/server.js:2:4

  `||`|||`|}}}}}}`
                  }}}}}}}}`|`}}}`       `...
     ~
      const ret = new Error(getStringFromWasm0(arg0, arg1));
                  ^
    at <anonymous> (file:///home/linuxbrew/.linuxbrew/Cellar/netlify-cli/16.4.2/libexec/lib/node_modules/netlify-cli/node_modules/@netlify/build/node_modules/@netlify/edge-bundler/deno/vendor/deno.land/x/eszip@v0.40.0/eszip_wasm.generated.js:417:19)

Bundling of edge function failed                              
────────────────────────────────────────────────────────────────

  Error message
  error: Uncaught (in promise) Error: The module's source code could not be parsed: Unexpected character '\u{7f}' at file:///root/.netlify/edge-functions/server/server.js:2:4

    `||`|||`|}}}}}}`
                    }}}}}}}}`|`}}}`     `...
       ~
        const ret = new Error(getStringFromWasm0(arg0, arg1));
                    ^
      at <anonymous> (file:///home/linuxbrew/.linuxbrew/Cellar/netlify-cli/16.4.2/libexec/lib/node_modules/netlify-cli/node_modules/@netlify/build/node_modules/@netlify/edge-bundler/deno/vendor/deno.land/x/eszip@v0.40.0/eszip_wasm.generated.js:417:19)

  Error location
  While bundling edge function

  Resolved config
  build:
    command: NITRO_PRESET=netlify-edge pnpm run build

This seems to be related specifically when using netlify build from the command line OR when an Automatic Deploy is trigger due to a git push.

Executing NITRO_PRESET=netlify-edge pnpm run build completes successfully as you can see form the logs.

Any thoughts?

I appreciate you!

Thanks!

Hey @petarmihaylov, thanks for posting this!

Judging from the error code, it looks like there’s an invalid character in the edge-function that’s compiled by Nitro.

I’ll try reproducing this locally, but in case that doesn’t work, could you maybe provide a reproduction case? Something like a ZIP with code where this fails, or a link to a public Git repository. That’d be super helpful for me to figure out what’s going on :slight_smile:

Hey @skn0tt, thanks for responding!

It appears the issue occurs when I include the nuxt-og-image module. It generates an image and the image appears to be included in the body of the Netlify Edge Functions. When the eszip module tries to compress the function, that’s where the error occurs.

The report at Petar Mihaylov / Netlify Reproduction · GitLab reproduces the issue.

The project is already configured to use the netlify-edge preset for the Nitro server.

I created two branches:

main is the code that reproduces the error when you run the netlify build command.
works is the branch where nuxt-og-image is disabled and netlify build completes successfully.


Given that this error appears to be related to how Netlify compresses the resulting Edge Functions, this issue is potentially reproducible with any NPM package that produces a binary file.

Please let me know how else I can help investigate.

Cheers!

Thanks for the writeup! Simon’s off this week but I left a note for him to look in when he gets back. I think you’re unblocked for now with your workaround, right?

Hey! Thanks for reaching out!

The nuxt-og-image module is vital to the project so removing/disabling it is not a viable workaround. If there is anything else I can do to help troubleshoot, I would be happy to help!

Thanks!

Meanwhile, I’d suggest you to switch to netlify preset instead of netlify-edge.

Hey @petarmihaylov! Back from my PTO. Thanks for providing the repro!

After looking into this, i’m pretty certain there’s a bug in what code nuxt-og-image generates. It’s inlining a WASM binary into the JS file, but is not encoding it. Here’s where that happens: https://github.com/harlan-zw/nuxt-og-image/blob/80984b3d97a56b960641b7f51014a6cce3998f27/src/module.ts#L550

Here’s a screenshot of the code that gets generated by this:

That’s not valid JavaScript, and looking at the nuxt-og-image source code here’s what should be changed to fix this:

  1. Base64-Encode the WASM binary before inlining it into the JS code, so it’s a valid JS string.
  2. Import Buffer, so that Buffer.from is available. (this is a Global in Node.js, but not in Deno, so it needs to be imported)

I hope that helps. I’d recommend you open an issue in the nuxt-og-image project about this, and work with the maintainers to get this issue resolved.

Let me know if there’s any other questions around this that I can help with :smiley: