Skip to content

Commit 23a80ff

Browse files
committed
Document the process.env handling and compat flags
Refs: cloudflare/workerd#3311
1 parent 38b029c commit 23a80ff

File tree

3 files changed

+62
-14
lines changed

3 files changed

+62
-14
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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-01-31"
9+
enable_flag: "nodejs_compat_populate_process_env"
10+
disable_flag: "nodejs_compat_do_not_populate_process_env"
11+
---
12+
13+
When the `nodejs_compat_populate_process_env` compatibility flag is used in conjunction with
14+
the `nodejs_compat` compatibility flag, all environment variables configured for the worker
15+
are added a string values to the `process.env` global.
16+
17+
Refer to the [environment variables](/workers/configuration/environment-variables/) docuemntation
18+
for more detail.

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

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

105105
<Render file="env_and_secrets" />
106106

107+
## Environment variables and Node.js compatibility
108+
109+
When using the `nodejs_compat` and `nodejs_compat_populate_process_env` compatibility
110+
flags, environment variables will also be available via the global `process.env`.
111+
112+
Text variable values are exposed directly.
113+
114+
JSON variable values that evaluate to string values are exposed as the parsed value.
115+
116+
JSON variable values that do not evaluate to string values are exposed as the raw
117+
JSON string.
118+
119+
For example, imagine a worker with three environment variables, one text value, and
120+
two JSON values:
121+
122+
* `FOO` with plaintext value `abc`,
123+
* `BAR` with JSON value `"abc"`,
124+
* `BAZ` with JSON value `"{ "a": 123 }"`
125+
126+
The value of `process.env.FOO` will be the JavaScript string `"abc"`.
127+
128+
The value of `process.env.BAR` will be the JavaScript string `"abc"` (note that the
129+
JSON encoding is removed).
130+
131+
The value of `process.env.BAZ` will be the JSON-encoded string `"{ "a": 123 }"`.
132+
133+
These rules mean that if you have a JSON environment variable whose value is a
134+
JSON-encoded string and you *want* to be able to parse it as JSON within your
135+
application (e.g. `JSON.parse(process.env.BAR)`) then the value will need to
136+
be "double encoded", for instance `'"\\"abc\\""'`.
137+
138+
Note also that because secrets are a form of environment variable within the runtime,
139+
secrets are also exposed via `process.env`.
140+
107141
## Related resources
108142

109143
* Learn how to access environment variables in [ES modules syntax](/workers/reference/migrate-to-module-workers/) for an optimized experience.

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ In the Node.js implementation of `process.env`, the `env` object is a copy of th
3030

3131
### Relationship to per-request `env` argument in `fetch()` handlers
3232

33-
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.
33+
Workers have a concept of [environment variables](/workers/configuration/environment-variables/) that are applied on a per-Worker and per-request basis.
34+
35+
By default, these are not accessible automatically via the `process.env` API. It is
36+
possible to have environment variables automatically populated into `process.env` using
37+
the `nodejs_compat_populate_process_env` compatibility flag. It is also possible to
38+
manually copy these values into `process.env` if necessary.
39+
40+
Setting any value on `process.env` will coerce that value into a string.
3441

3542
```js
3643
import * as process from 'node:process';
@@ -48,19 +55,8 @@ export default {
4855
};
4956
```
5057
51-
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.
52-
53-
```js
54-
import * as process from 'node:process';
55-
import { env } from 'node:process';
56-
57-
process.env === env; // true! they are the same object
58-
process.env = {}; // replace the object! Do not do this!
59-
process.env === env; // false! they are no longer the same object
60-
61-
// From this point forward, any changes to process.env will not be reflected in env,
62-
// and vice versa!
63-
```
58+
Refer to the [environment variables](/workers/configuration/environment-variables/)
59+
documentation for more detail.
6460
6561
## `process.nextTick()`
6662

0 commit comments

Comments
 (0)