More NPM packages on Cloudflare Workers: Combining polyfills and native code to support Node.js APIs
Today, we are excited to announce a preview of improved Node.js compatibility for Workers and Pages. Broader compatibility lets you use more NPM packages and take advantage of the JavaScript ecosystem when writing your Workers.
Our newest version of Node.js compatibility combines the best features of our previous efforts. Cloudflare Workers have supported Node.js in some form for quite a while. We first announced polyfill support in 2021, and later built-in support for parts of the Node.js API that has expanded over time.
The latest changes make it even better:
You can use far more NPM packages on Workers.
You can use packages that do not use the
node
: prefix to import Node.js APIsYou can use more Node.js APIs on Workers, including most methods on
async_hooks
,buffer
,dns
,os
, andevents
. Many more, such asfs
orprocess
are importable with mocked methods.
To give it a try, add the following flag to wrangler.toml
, and deploy your Worker with Wrangler:
compatibility_flags = ["nodejs_compat_v2"]
Packages that could not be imported with nodejs_compat
, even as a dependency of another package, will now load. This Continue reading