Skip to content

Commit c0c4aa8

Browse files
committed
Adds changelog and flag information for process.env population
1 parent 5d4194d commit c0c4aa8

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: Access environment variables on process.env in you Worker
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-07T11: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 variable 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 added when Node.js compatability was turned on, but it was empty.
23+
This could cause unexpected errors or friction when developing Workers with code previously written for
24+
Node.js.
25+
26+
Now, [environment variables](/workers/configuration/environment-variables/),
27+
[secrets](/workers/configuration/secrets/), and [version metadata](/workers/runtime-apis/bindings/version-metadata/)
28+
can all be accessed on `process.env`.
29+
30+
After April 1, 2025, populating `process.env` will become the default behavior when `nodejs_compat` is enabled, and
31+
your Worker's compatability date is after "2025-04-01". Until then, users can opt-in to this behavior by adding the
32+
[`nodejs_compat_populate_process_env`](/workers/configuration/compatibility-flags/#nodejs-compat-populate-process-env)
33+
compatability flag.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
name: "Enable `cache: no-store` HTTP standard API"
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 [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 `proecess.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`. In this case, `process.env` will still be available,
25+
but will not have values automatically added.
26+
27+
This flag will default to on for any compatability dates after `2025-04-01`.

0 commit comments

Comments
 (0)