Cloudflare Docs
Workers
Visit Workers on GitHub
Set theme to dark (⇧+D)

Node.js compatibility

The following APIs from the Node.js runtime are available directly as Runtime APIs, with no need to add polyfills to your own code:

Node.js APIs are available under the node: prefix, and this prefix must be used when importing modules, both in your code and the NPM packages you depend on.

// Do this:
import { Buffer } from 'node:buffer';
// Do not do this:
import { Buffer } from 'buffer';

​​ Enable Node.js with Workers

Add the nodejs_compat compatibility flag to your wrangler.toml:

wrangler.toml
compatibility_flags = [ "nodejs_compat" ]

​​ Enable Node.js with Pages Functions

​​ Enable Node.js with Wrangler

To enable nodejs_compat in local development, pass the --compatibility-flags argument with the nodejs_compat flag to wrangler pages dev:

$ wrangler pages dev [<DIRECTORY>] --compatibility-flags="nodejs_compat" --experimental-local

For additional options, refer to the list of Pages-specific CLI commands.

​​ Enable Node.js from the Cloudflare dashboard

To enable Node.js for your Pages Function from the Cloudflare dashboard:

  1. Log into the Cloudflare dashboard and select your account.
  2. Select Pages and select your Pages project.
  3. Select Settings > Functions > Compatibility Flags.
  4. Add the nodejs_compat compatibility flag to your Preview and Production deployments.