Skip to content

Commit e5285f0

Browse files
authored
Document the process.env handling and compat flags (#19187)
Refs: cloudflare/workerd#3311
1 parent 285d6d3 commit e5285f0

File tree

4 files changed

+62
-5
lines changed

4 files changed

+62
-5
lines changed

src/content/compatibility-flags/nodejs-compat-populate-process-env.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,6 @@ reduce toil and help with compatibility for existing Node.js libraries.
2323
If users do not wish for these values to be accessible via `process.env`, they can use the
2424
`nodejs_compat_do_not_populate_process_env` flag. In this case, `process.env` will still be
2525
available, but will not have values automatically added.
26+
27+
If the `disallow_importable_env` compatibility flag is set, the `process.env` will also
28+
not be populated.

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

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

100100
<Render file="secrets-in-dev" />
101101

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

104146
- 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ 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. Secrets are used for storing sensitive information like API keys and auth tokens. Secrets are programmatically available on the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters) passed to your Worker's [`fetch` event handler](/workers/runtime-apis/handlers/fetch/), and may also be accessible via [`process.env`](/workers/configuration/environment-variables) in Workers that support Node.js compatibility.
1313

1414
## Access your secrets with Workers
1515

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

113113
- [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.
114+
- [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.
39+
To automatically populate environment variables and secrets on `process.env`, enable
40+
the [`nodejs_compat_populate_process_env`](/workers/configuration/compatibility-flags/#nodejs_compat_populate_process_env)
41+
compatibility flag and disable the `disallow_importable_env` compatibility flag.
42+
It is also possible to manually copy these values into `process.env` if
43+
necessary -- but only within the context of 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)