Skip to content

Commit 9220bba

Browse files
committed
Document the process.env handling and compat flags
Refs: cloudflare/workerd#3311
1 parent 70ea5a9 commit 9220bba

File tree

4 files changed

+82
-5
lines changed

4 files changed

+82
-5
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
_build:
3+
publishResources: false
4+
render: never
5+
list: never
6+
7+
name: "Populate process.env from environment variables"
8+
sort_date: "2025-04-01"
9+
enable_flag: "nodejs_compat_populate_process_env"
10+
enable_date: "2025-04-01"
11+
disable_flag: "nodejs_compat_do_not_populate_process_env"
12+
---
13+
14+
When the `nodejs_compat_populate_process_env` compatibility flag is used in conjunction with
15+
the `nodejs_compat` compatibility flag, all environment variables configured for the
16+
Worker are added as string values to the `process.env` global.
17+
18+
If the `disallow_importable_env` compatibility flag is set, the `process.env`
19+
will not be populated.
20+
21+
Refer to the [environment variables](/workers/configuration/environment-variables/) documentation
22+
for more detail.

src/content/docs/workers/configuration/environment-variables.mdx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,48 @@ Select the **Secret** type if your environment variable is a [secret](/workers/c
9797

9898
<Render file="secrets-in-dev" />
9999

100+
## Environment variables and Node.js compatibility
101+
102+
When you enable both the [`nodejs_compat`](/workers/runtime-apis/nodejs/) and
103+
[`nodejs_compat_populate_process_env`](/workers/configuration/compatibility-flags/)
104+
compatibility flags, and the `disallow_importable_env` compatibility flag is
105+
not set, environment variables will also be available via the global
106+
`process.env`. Note that the `nodejs_compat_populate_process_env` flag is
107+
enabled automatically when `nodejs_compat` is used with a compatibility date
108+
on or after April 1st, 2025.
109+
110+
The `process.env` will be populated lazily the first time that `process` is accessed
111+
in the worker.
112+
113+
Text variable values are exposed directly.
114+
115+
JSON variable values that evaluate to string values are exposed as the parsed value.
116+
117+
JSON variable values that do not evaluate to string values are exposed as the raw
118+
JSON string.
119+
120+
For example, imagine a Worker with three environment variables, two text values, and
121+
one JSON value:
122+
123+
```
124+
[vars]
125+
FOO = "abc"
126+
BAR = "abc"
127+
BAZ = { "a": 123 }
128+
```
129+
130+
Environment variables can be added using either the `wrangler.{json|jsonc|toml}` file or via the Cloudflare
131+
dashboard UI.
132+
133+
The values of `process.env.FOO` and `process.env.BAR` will each be the
134+
JavaScript string `"abc"`.
135+
136+
The value of `process.env.BAZ` will be the JSON-encoded string `"{ \"a\": 123 }"`.
137+
138+
:::note
139+
Note also that because secrets are a form of environment variable within the runtime,
140+
secrets are also exposed via `process.env`.
141+
100142
## Related resources
101143

102144
- Migrating environment variables from [Service Worker format to ES modules syntax](/workers/reference/migrate-to-module-workers/#environment-variables).

src/content/docs/workers/configuration/secrets.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { Render } from "~/components";
99

1010
## Background
1111

12-
Secrets are a type of binding that allow you to attach encrypted text values to your Worker. You cannot see secrets after you set them and can only access secrets via [Wrangler](/workers/wrangler/commands/#secret) or programmatically via the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters). Secrets are used for storing sensitive information like API keys and auth tokens. Secrets are available on the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters) passed to your Worker's [`fetch` event handler](/workers/runtime-apis/handlers/fetch/).
12+
Secrets are a type of binding that allow you to attach encrypted text values to your Worker. You cannot see secrets after you set them and can only access secrets via [Wrangler](/workers/wrangler/commands/#secret) or programmatically via the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters). Secrets are used for storing sensitive information like API keys and auth tokens. Secrets are available on the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters) passed to your Worker's [`fetch` event handler](/workers/runtime-apis/handlers/fetch/),
13+
and may also be accessible via `process.env` in Workers that support Node.js compatibility.
1314

1415
## Access your secrets with Workers
1516

@@ -111,4 +112,4 @@ To delete a secret from your Worker project via the dashboard:
111112
## Related resources
112113

113114
- [Wrangler secret commands](/workers/wrangler/commands/#secret) - Review the Wrangler commands to create, delete and list secrets.
114-
- [Cloudflare Secrets Store](/secrets-store/) - Encrypt and store sensitive information as secrets that are securely reusable across your account.
115+
- [Cloudflare Secrets Store](/secrets-store/) - Encrypt and store sensitive information as secrets that are securely reusable across your account.

src/content/docs/workers/runtime-apis/nodejs/process.mdx

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,16 @@ When [Node.js compatibility](/workers/runtime-apis/nodejs/) is turned on and the
3333

3434
### Relationship to per-request `env` argument in `fetch()` handlers
3535

36-
Workers do have a concept of [environment variables](/workers/configuration/environment-variables/) that are applied on a per-Worker and per-request basis. These are not accessible automatically via the `process.env` API. It is possible to manually copy these values into `process.env` if you need to. Be aware, however, that setting any value on `process.env` will coerce that value into a string.
36+
Workers have a concept of [environment variables](/workers/configuration/environment-variables/) that are applied on a per-Worker and per-request basis.
37+
38+
By default, these are not accessible via the `process.env` API. It is
39+
To automatically populate environment variables and secrets on `process.env`, enable
40+
the `nodejs_compat_populate_process_env` compatibility flag and disable the
41+
`disallow_importable_env` compatibility flag. It is also possible to
42+
manually copy these values into `process.env` if necessary -- but only within the context of
43+
a request.
44+
45+
Setting any value on `process.env` will coerce that value into a string.
3746

3847
```js
3948
import * as process from 'node:process';
@@ -51,7 +60,11 @@ export default {
5160
};
5261
```
5362
54-
It is strongly recommended that you *do not* replace the entire `process.env` object with the request `env` object. Doing so will cause you to lose any environment variables that were set previously and will cause unexpected behavior for other Workers running in the same isolate. Specifically, it would cause inconsistency with the `process.env` object when accessed via named imports.
63+
It is strongly recommended that you *do not* replace the entire `process.env` object with
64+
the cloudflare `env` object. Doing so will cause you to lose any environment variables that
65+
were set previously and will cause unexpected behavior for other Workers running in the
66+
same isolate. Specifically, it would cause inconsistency with the `process.env` object when
67+
accessed via named imports.
5568
5669
```js
5770
import * as process from 'node:process';
@@ -63,7 +76,6 @@ process.env === env; // false! they are no longer the same object
6376

6477
// From this point forward, any changes to process.env will not be reflected in env,
6578
// and vice versa!
66-
```
6779

6880
## `process.nextTick()`
6981

0 commit comments

Comments
 (0)