101arrowz / fflate

High performance (de)compression in an 8kB package
https://101arrowz.github.io/fflate
MIT License
2.21k stars 77 forks source link

Cloudflare Workers deploy fails because it cannot resolve "module" #220

Closed serban-mihai closed 3 weeks ago

serban-mihai commented 1 month ago

Although the build of the worker ends well, when trying to deploy with wrangler it crashes with:

  ✘ [ERROR] Could not resolve "module"

      dist/index.js:1580:30:
        1580 │ import { createRequire } from "module";
             ╵                               ~~~~~~~~

    The package "module" wasn't found on the file system but is built into node.
    Add "node_compat = true" to your wrangler.toml file to enable Node.js compatibility.

  ✘ [ERROR] Build failed with 1 error:

    dist/index.js:1580:30: ERROR: Could not resolve "module"

The nodejs_compat flag is set for the worker so it's not because of that. The library works locally of course but since it's supposed to run even in browsers I can't understand why CF Workers reject it, It might be because the built environment is treated as server and that this version has that "module" lib that requires Node.js and CF Workers don't have a polyfill for it, but I'm wondering how can we deploy this to CF, it's really tiny and it works like a charm. There is also no additional configuration of esbuild or anything else.

Also by adding node_compat = true in wrangler.toml instead of the compatibility_flag the error turns to:

  ✘ [ERROR] No matching export in "node-modules-polyfills:module" for import "createRequire"

      dist/index.js:1580:9:
        1580 │ import { createRequire } from "module";
             ╵          ~~~~~~~~~~~~~

  ✘ [ERROR] Build failed with 1 error:

    dist/index.js:1580:9: ERROR: No matching export in "node-modules-polyfills:module" for import "createRequire"

Only adding the library to the workers project and import any method from it such as import { zipSync } from "fflate"; causes the deploy error. Do you have any idea on how to make the library work on Cloudflare Workers?

serban-mihai commented 3 weeks ago

My bad, it was a custom build config that I was using for building the Worker that was causing the error, just use the default build command for wrangler and it works like a charm