CloudflareLogan GatlinExplainer
How we rebuilt Cloudflare Workers’ module registry for Node.js compatibility
Summary
Cloudflare has rebuilt the module registry in workerd, the open-source core of the Workers runtime, to improve speed, standards compliance, and alignment with Node.js. The change addresses compatibility beyond runtime APIs by making module resolution, loading, and caching behave more like Node.js across ESM, CommonJS, and WebAssembly. The new registry uses URLs as specifiers, supports import.meta.url, import.meta.main, and import.meta.resolve(), distinguishes modules by query strings and fragments, validates import attributes, follows require(esm) rules, and compiles modules lazily while sharing caches across isolates. It also supports WebAssembly source phase imports and consistent loading errors; the existing registry remains in place, and the new implementation must be enabled with the new_module_registry compatibility flag.
Context
Workers needed compatibility with Node.js applications beyond individual runtime APIs. Module resolution, loading, and caching behavior also affects applications, while the original registry used filesystem-style paths, compiled the entire bundle up front, and kept separate copies per V8 isolate.
Approach / What changed
The new workerd registry uses URLs as module specifiers and builds in lazy compilation and cache sharing. It adds Node.js-aligned behavior for import.meta APIs, URL-based resolution, module identity, import attributes, require(esm), error handling, and WebAssembly source phase imports. Workers can opt in with the new_module_registry compatibility flag.
Takeaways
Module specifiers are parsed as URLs, so relative imports follow new URL() resolution rules; query strings and fragments create distinct module instances.
The registry compiles modules when first imported, whether statically or dynamically, rather than compiling the entire Worker bundle up front.
WebAssembly source phase imports return a WebAssembly.Module directly, but source phase imports for other module types currently throw a SyntaxError.
Source and freshness
- Source
- blog.cloudflare.com
- Published
- Added to Yomu
Summaries are generated from the original by a language model. Check the original before relying on it.
Keep reading
Related reading
The Good Stuff
Paid picks we use ourselves. Links carry a referral code — a subscription through one earns us a commission at no extra cost to you.
The best engineering reads, once a week
One useful email. Unsubscribe anytime.