-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Adds changelog and flag information for process.env population #20642
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b3ebcc4
Adds changelog and flag information for process.env population
mikenomitch c2528f7
Updates title of compat flag
mikenomitch 5d5fd07
Wording tweaks and added to main process env docs
mikenomitch 2562375
Fixing changelog time
mikenomitch 0655d8f
Updates link to process env docs
mikenomitch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/content/changelog/workers/2025-03-11-process-env-support.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| --- | ||
| title: Access your Worker's environment variables from process.env | ||
| description: With Node.js compatability on, process.env is automatically populated with environment variables and secrets | ||
| products: | ||
| - workers | ||
| date: 2025-03-11T15:00:00Z | ||
| --- | ||
|
|
||
| You can now access [environment variables](/workers/configuration/environment-variables/) and | ||
| [secrets](/workers/configuration/secrets/) on [`process.env`](/workers/runtime-apis/nodejs/process/#processenv) | ||
| when using the [`nodejs_compat` compatability flag](/workers/configuration/compatibility-flags/#nodejs-compatibility-flag). | ||
|
|
||
| ```js | ||
| const apiClient = ApiClient.new({ apiKey: process.env.API_KEY }); | ||
| const LOG_LEVEL = process.env.LOG_LEVEL || "info"; | ||
| ``` | ||
|
|
||
| In Node.js, environment variables are exposed via the global `process.env` object. Some libraries | ||
| assume that this object will be populated, and many developers may be used to accessing variables | ||
| in this way. | ||
|
|
||
| Previously, the `process.env` object was always empty unless written to in Worker code. This could | ||
| cause unexpected errors or friction when developing Workers using code previously written for Node.js. | ||
|
|
||
| Now, [environment variables](/workers/configuration/environment-variables/), | ||
| [secrets](/workers/configuration/secrets/), and [version metadata](/workers/runtime-apis/bindings/version-metadata/) | ||
| can all be accessed on `process.env`. | ||
|
|
||
| After April 1, 2025, populating `process.env` will become the default behavior when `nodejs_compat` is enabled, and | ||
| your Worker's compatability date is after "2025-04-01". Until then, users can opt-in to this behavior by adding the | ||
| [`nodejs_compat_populate_process_env`](/workers/configuration/compatibility-flags/#enable-auto-populating-processenv) | ||
| compatability flag. |
25 changes: 25 additions & 0 deletions
25
src/content/compatibility-flags/nodejs-compat-populate-process-env.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| --- | ||
| name: "Enable auto-populating `process.env`" | ||
| sort_date: "2025-02-27" | ||
| enable_date: "2025-04-01" | ||
| enable_flag: "nodejs_compat_populate_process_env" | ||
| disable_flag: "nodejs_compat_do_not_populate_process_env" | ||
| --- | ||
|
|
||
| When you enable the `nodejs_compat_populate_process_env` compatibility flag and the [`nodejs_compat`](/workers/runtime-apis/nodejs/) | ||
| flag is also enabled, `process.env` will be populated with values from any bindings with text or JSON values. | ||
| This means that if you have added [environment variables](/workers/configuration/environment-variables/), | ||
| [secrets](/workers/configuration/secrets/), or [version metadata](/workers/runtime-apis/bindings/version-metadata/) | ||
| bindings, these values can be accessed on `process.env`. | ||
|
|
||
| ```js | ||
| const apiClient = ApiClient.new({ apiKey: process.env.API_KEY }); | ||
| const LOG_LEVEL = process.env.LOG_LEVEL || "info"; | ||
| ``` | ||
|
|
||
| This makes accessing these values easier and conforms to common Node.js patterns, which can | ||
| reduce toil and help with compatability for existing Node.js libraries. | ||
|
|
||
| If users do not wish for these values to be accessible via `process.env`, they can use the | ||
| `nodejs_compat_do_not_populate_process_env` flag. In this case, `process.env` will still be | ||
| available, but will not have values automatically added. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.