diff --git a/src/content/docs/workers/configuration/compatibility-flags.mdx b/src/content/docs/workers/configuration/compatibility-flags.mdx index 7ffa42342644dda..a0d79290eaaaf59 100644 --- a/src/content/docs/workers/configuration/compatibility-flags.mdx +++ b/src/content/docs/workers/configuration/compatibility-flags.mdx @@ -6,7 +6,7 @@ head: [] description: Opt into a specific features of the Workers runtime for your Workers project. --- -import { CompatibilityFlags, WranglerConfig } from "~/components"; +import { CompatibilityFlags, WranglerConfig, Render } from "~/components"; Compatibility flags enable specific features. They can be useful if you want to help the Workers team test upcoming changes that are not yet enabled by default, or if you need to hold back a change that your code depends on but still want to apply other compatibility changes. @@ -43,7 +43,14 @@ Compatibility flags can be set when uploading a Worker using the [Workers Script ## Node.js compatibility flag -import { Render } from "~/components"; +:::note +[The `nodejs_compat` flag](/workers/runtime-apis/nodejs/) also enables `nodejs_compat_v2` as long as your compatibility date is 2024-09-23 or later. The v2 flag improves runtime Node.js compatibility by bundling additional polyfills and globals into your Worker. However, this improvement increases bundle size. + +If your compatibility date is 2024-09-22 or before and you want to enable v2, add the `nodejs_compat_v2` in addition to the `nodejs_compat` flag. +If your compatibility date is after 2024-09-23, but you want to disable v2 to avoid increasing your bundle size, add the `no_nodejs_compat_v2` in addition to the `nodejs_compat flag`. +::: + +A [growing subset](/workers/runtime-apis/nodejs/) of Node.js APIs are available directly as [Runtime APIs](/workers/runtime-apis/nodejs), with no need to add polyfills to your own code. To enable these APIs in your Worker, add the `nodejs_compat` compatibility flag to your `wrangler.toml`: diff --git a/src/content/docs/workers/runtime-apis/nodejs/index.mdx b/src/content/docs/workers/runtime-apis/nodejs/index.mdx index c935e4eba65820c..7946952a7d92166 100644 --- a/src/content/docs/workers/runtime-apis/nodejs/index.mdx +++ b/src/content/docs/workers/runtime-apis/nodejs/index.mdx @@ -18,9 +18,17 @@ You can view which APIs are supported using the [Node.js compatibility matrix](h ## Get Started -import { Render } from "~/components"; +To enable built-in Node.js APIs and add polyfills, you need to add the `nodejs_compat` compatibility flag to your wrangler configuration. This also enables `nodejs_compat_v2` as long as your compatibility date is 2024-09-23 or later. [Learn more about the Node.js compatibility flag and v2](/workers/configuration/compatibility-flags/#nodejs-compatibility-flag). - + + + +```toml title="wrangler.toml" +compatibility_flags = [ "nodejs_compat" ] +compatibility_date = "2024-09-23" +``` + + ## Built-in Node.js Runtime APIs @@ -32,7 +40,7 @@ Unless otherwise specified, implementations of Node.js APIs in Workers are inten ## Node.js API Polyfills -When you enable the `nodejs_compat` compatibility flag and set your compatibility date to `2024-09-23` or later, in addition to enabling Node.js APIs in the Workers Runtime, [Wrangler](/workers/wrangler/) will use [unenv](https://github.com/unjs/unenv) to automatically detect uses of Node.js APIs, and add polyfills where relevant. +To enable built-in Node.js APIs and add polyfills, you need to add the `nodejs_compat` compatibility flag to your wrangler configuration. This also enables `nodejs_compat_v2` as long as your compatibility date is 2024-09-23 or later. [Learn more about the Node.js compatibility flag and v2](/workers/configuration/compatibility-flags/#nodejs-compatibility-flag). Adding polyfills maximizes compatibility with existing npm packages, while recognizing that not all APIs from Node.js make sense in the context of serverless functions.