Skip to content

Commit dc4ef68

Browse files
authored
Adds changelog and flag information for process.env population (#20642)
Adds changelog and flag information for process.env population
1 parent 337158d commit dc4ef68

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Access your Worker's environment variables from process.env
3+
description: With Node.js compatability on, process.env is automatically populated with environment variables and secrets
4+
products:
5+
- workers
6+
date: 2025-03-11T15:00:00Z
7+
---
8+
9+
You can now access [environment variables](/workers/configuration/environment-variables/) and
10+
[secrets](/workers/configuration/secrets/) on [`process.env`](/workers/runtime-apis/nodejs/process/#processenv)
11+
when using the [`nodejs_compat` compatability flag](/workers/configuration/compatibility-flags/#nodejs-compatibility-flag).
12+
13+
```js
14+
const apiClient = ApiClient.new({ apiKey: process.env.API_KEY });
15+
const LOG_LEVEL = process.env.LOG_LEVEL || "info";
16+
```
17+
18+
In Node.js, environment variables are exposed via the global `process.env` object. Some libraries
19+
assume that this object will be populated, and many developers may be used to accessing variables
20+
in this way.
21+
22+
Previously, the `process.env` object was always empty unless written to in Worker code. This could
23+
cause unexpected errors or friction when developing Workers using code previously written for Node.js.
24+
25+
Now, [environment variables](/workers/configuration/environment-variables/),
26+
[secrets](/workers/configuration/secrets/), and [version metadata](/workers/runtime-apis/bindings/version-metadata/)
27+
can all be accessed on `process.env`.
28+
29+
After April 1, 2025, populating `process.env` will become the default behavior when `nodejs_compat` is enabled, and
30+
your Worker's compatability date is after "2025-04-01". Until then, users can opt-in to this behavior by adding the
31+
[`nodejs_compat_populate_process_env`](/workers/configuration/compatibility-flags/#enable-auto-populating-processenv)
32+
compatability flag.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
name: "Enable auto-populating `process.env`"
3+
sort_date: "2025-02-27"
4+
enable_date: "2025-04-01"
5+
enable_flag: "nodejs_compat_populate_process_env"
6+
disable_flag: "nodejs_compat_do_not_populate_process_env"
7+
---
8+
9+
When you enable the `nodejs_compat_populate_process_env` compatibility flag and the [`nodejs_compat`](/workers/runtime-apis/nodejs/)
10+
flag is also enabled, `process.env` will be populated with values from any bindings with text or JSON values.
11+
This means that if you have added [environment variables](/workers/configuration/environment-variables/),
12+
[secrets](/workers/configuration/secrets/), or [version metadata](/workers/runtime-apis/bindings/version-metadata/)
13+
bindings, these values can be accessed on `process.env`.
14+
15+
```js
16+
const apiClient = ApiClient.new({ apiKey: process.env.API_KEY });
17+
const LOG_LEVEL = process.env.LOG_LEVEL || "info";
18+
```
19+
20+
This makes accessing these values easier and conforms to common Node.js patterns, which can
21+
reduce toil and help with compatability for existing Node.js libraries.
22+
23+
If users do not wish for these values to be accessible via `process.env`, they can use the
24+
`nodejs_compat_do_not_populate_process_env` flag. In this case, `process.env` will still be
25+
available, but will not have values automatically added.

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ nextTick(() => {
2626

2727
## `process.env`
2828

29-
In the Node.js implementation of `process.env`, the `env` object is a copy of the environment variables at the time the process was started. In the Workers implementation, there is no process-level environment, so `env` is an empty object. You can still set and get values from `env`, and those will be globally persistent for all Workers running in the same isolate and context (for example, the same Workers entry point).
29+
In the Node.js implementation of `process.env`, the `env` object is a copy of the environment variables at the time the process was started. In the Workers implementation, there is no process-level environment, so by default `env` is an empty object. You can still set and get values from `env`, and those will be globally persistent for all Workers running in the same isolate and context (for example, the same Workers entry point).
30+
31+
When [Node.js compatability](/workers/runtime-apis/nodejs/) is turned on and the [`nodejs_compat_populate_process_env`](/workers/configuration/compatibility-flags/#enable-auto-populating-processenv) compatability flag is set, `process.env` will contain any [environment variables](/workers/configuration/environment-variables/),
32+
[secrets](/workers/configuration/secrets/), or [version metadata](/workers/runtime-apis/bindings/version-metadata/) metadata that has been configured on your Worker.
3033

3134
### Relationship to per-request `env` argument in `fetch()` handlers
3235

0 commit comments

Comments
 (0)