Cloudflare DevelopersCD
Cloudflare Developers4mo ago
3 replies
cubeghost

Providing external dependencies to Worker Loader modules

Is there a way to pass through external dependencies (from node_modules) to a dynamic Worker's modules?

for example:
import mod from "@happy-dom/global-registrator";

const worker = env.LOADER.get(renderId, () => {
  return {
    compatibilityDate: "2025-09-21",
    compatibilityFlags: ["nodejs_compat"],
    mainModule: "render.js",
    modules: {
      "render.js": `
        import {WorkerEntrypoint} from "cloudflare:workers";
        import {GlobalRegistrator} from "@happy-dom/global-registrator";

        export class RenderEntrypoint extends WorkerEntrypoint {
          async render() {
            GlobalRegistrator.register();

            // do something with document assigned to globalThis

            await GlobalRegistrator.unregister();

            return;
          }
        }`,
      "@happy-dom/global-registrator": mod // how am I supposed to pass in the module here? 
    },
    globalOutbound: null
  };
});

const entrypoint = worker.getEntrypoint("RenderEntrypoint");
const result = await entrypoint.render();
Was this page helpful?