Thanks to visit codestin.com
Credit goes to developers.cloudflare.com

Skip to content
Cloudflare Docs

Node.js compatibility

When you write a Worker, you may need to import packages from npm โ†—. Many npm packages rely on APIs from the Node.js runtime โ†—, and will not work unless these Node.js APIs are available.

Cloudflare Workers provides a subset of Node.js APIs in two forms:

  1. As built-in APIs provided by the Workers Runtime. Most of these APIs are full implementations of the corresponding Node.js APIs, while a few are partially supported or non-functional stubs intended for the APIs to be available for import only but not for actual use.
  2. As polyfill shim implementations that Wrangler adds to your Worker's code, allowing it to import the module, but calling API methods will throw errors.

Get Started

To enable built-in Node.js APIs and add polyfills, add the nodejs_compat compatibility flag to your wrangler configuration file, and ensure that your Worker's compatibility date is 2024-09-23 or later. Learn more about the Node.js compatibility flag and v2.

{
"$schema": "./node_modules/wrangler/config-schema.json",
"compatibility_flags": [
"nodejs_compat"
],
"compatibility_date": "2024-09-23"
}

Supported Node.js APIs

The runtime APIs from Node.js listed below as "๐ŸŸข supported" are currently natively supported in the Workers Runtime. Item listed as "๐ŸŸก partially supported" are either only partially implemented or are implemented as non-functional stubs.

Deprecated or experimental APIs from Node.js โ†—, and APIs that do not fit in a serverless context, are not included as part of the list below:

API NameNatively supported by the Workers Runtime
Assertion testing๐ŸŸข supported
Asynchronous context tracking๐ŸŸข supported
Async hooks โ†—๐ŸŸก partially supported (non-functional)
Buffer๐ŸŸข supported
Child processes โ†—๐ŸŸก partially supported (non-functional)
Cluster โ†—๐ŸŸก partially supported (non-functional)
Console โ†—๐ŸŸก partially supported
Crypto๐ŸŸข supported
Debugger๐ŸŸข supported via Chrome Dev Tools integration
Diagnostics Channel๐ŸŸข supported
DNS๐ŸŸข supported
Errors๐ŸŸข supported
Events๐ŸŸข supported
File system๐ŸŸข supported
Globals๐ŸŸข supported
HTTP๐ŸŸข supported
HTTP/2 โ†—๐ŸŸก partially supported (non-functional)
HTTPS๐ŸŸข supported
Inspector โ†—๐ŸŸก partially supported via Chrome Dev Tools integration
Module โ†—๐ŸŸก partially supported
Net๐ŸŸข supported
OS โ†—๐ŸŸก partially supported
Path๐ŸŸข supported
Performance hooks โ†—๐ŸŸก partially supported
Process๐ŸŸข supported
Punycode โ†— (deprecated)๐ŸŸข supported
Readline โ†—๐ŸŸก partially supported (non-functional)
REPL โ†—๐ŸŸก partially supported (non-functional)
Query strings โ†—๐ŸŸข supported
SQLite โ†—โšช not yet supported
Stream๐ŸŸข supported
String decoder๐ŸŸข supported
Test runner โ†—โšช not supported
Timers๐ŸŸข supported
TLS/SSL๐ŸŸก partially supported
UDP/datagram โ†—๐ŸŸก partially supported (non-functional)
URL๐ŸŸข supported
Utilities๐ŸŸข supported
V8 โ†—๐ŸŸก partially supported (non-functional)
VM โ†—๐ŸŸก partially supported (non-functional)
Web Crypto API๐ŸŸข supported
Web Streams API๐ŸŸข supported
Zlib๐ŸŸข supported

Unless otherwise specified, native implementations of Node.js APIs in Workers are intended to match the implementation in the Current release of Node.js โ†—.

If an API you wish to use is missing and you want to suggest that Workers support it, please add a post or comment in the Node.js APIs discussions category โ†— on GitHub.

Node.js API Polyfills

Node.js APIs that are not yet supported in the Workers runtime are polyfilled via Wrangler, which uses unenv โ†—. If the nodejs_compat compatibility flag is enabled, and your Worker's compatibility date is 2024-09-23 or later, Wrangler will automatically inject polyfills into your Worker's code.

Adding polyfills maximizes compatibility with existing npm packages by providing modules with mocked methods. Calling these mocked methods will either noop or will throw an error with a message like:

[unenv] <method name> is not implemented yet!

This allows you to import packages that use these Node.js modules, even if certain methods are not supported.

Enable only AsyncLocalStorage

If you need to enable only the Node.js AsyncLocalStorage API, you can enable the nodejs_als compatibility flag:

{
"$schema": "./node_modules/wrangler/config-schema.json",
"compatibility_flags": [
"nodejs_als"
]
}