What is Node.js compatibility?
Node.js compatibility is a layer that implements a subset of the Node.js API in workerd. When you enable thenodejs_compat flag, you can import Node.js built-in modules using the node: prefix:
Enable Node.js compatibility
You enable Node.js compatibility using compatibility flags in your configuration:Compatibility flags
The
nodejs_compat_v2 flag implies nodejs_compat and adds additional features like global Buffer and process objects.nodejs_compat: Enables Node.js module imports with thenode:prefixnodejs_compat_v2: Extendsnodejs_compatwith:- Imports without the
node:prefix (for example,import { Buffer } from 'buffer') - Global
Bufferandprocessobjects available everywhere
- Imports without the
nodejs_compat automatically implies nodejs_compat_v2.
AsyncLocalStorage
You can enableAsyncLocalStorage independently of full Node.js compatibility:
node:async_hooks for context management without enabling all Node.js APIs.
Architecture
Node.js compatibility in workerd consists of two layers:C++ implementation layer
Located insrc/workerd/api/node/, this layer provides:
- Native implementations of performance-critical APIs (buffer operations, crypto, zlib)
- Low-level bindings to system libraries (BoringSSL for crypto, zlib for compression)
- Integration with workerd’s runtime primitives
NODEJS_MODULES macro in node.h and exposed as node-internal:* specifiers.
TypeScript implementation layer
Located insrc/node/, this layer provides:
- Public
node:*module exports that you import in your code - JavaScript implementations of Node.js APIs built on C++ primitives
- Compatibility shims and feature detection
node-internal:* modules and re-exports them as standardized node:* APIs.
Compatibility philosophy
workerd follows these principles for Node.js compatibility:- Best-effort implementation: workerd implements a subset of the Node.js API as closely as possible, but does not aim for 100% compatibility
- Web standards take precedence: When Node.js implements Web Platform Standard APIs differently, workerd follows the standard specification first
- Backwards compatibility: Changes to Node.js compatibility require explicit opt-in through compatibility flags to avoid breaking existing Workers
- Runtime constraints: Some Node.js APIs cannot be implemented in workerd due to architectural differences (for example, filesystem access, child processes)
- Incremental improvements: Partial implementations can be merged and evolved over time, with compatibility flags protecting against breaking changes
Module gating
Many Node.js modules are gated behind specific compatibility flags. For example:node:httpandnode:httpsrequireenable_nodejs_http_modulesnode:fsrequiresenable_nodejs_fs_modulenode:osrequiresenable_nodejs_os_module
nodejs_compat enabled, workerd automatically enables flags based on that date. Check the supported APIs page for module availability.
Next steps
Supported APIs
View the complete list of supported Node.js modules and APIs
Differences from Node.js
Learn about key differences and limitations