Skip to content

Document the process.env handling and compat flags #19187

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

Open
wants to merge 13 commits into
base: production
Choose a base branch
from

Conversation

jasnell
Copy link
Contributor

@jasnell jasnell commented Jan 14, 2025

Refs: cloudflare/workerd#3311

Summary

Screenshots (optional)

Documentation checklist

  • The documentation style guide has been adhered to.
  • If a larger change - such as adding a new page- an issue has been opened in relation to any incorrect or out of date information that this PR fixes.
  • Files which have changed name or location have been allocated redirects.

hyperlint-ai[bot]

This comment was marked as outdated.

Co-authored-by: hyperlint-ai[bot] <154288675+hyperlint-ai[bot]@users.noreply.github.com>
Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added inline comments.

It would be great to add details to https://developers.cloudflare.com/workers/configuration/secrets/ -> mention the flag and process.env

Thanks

@vicb
Copy link
Contributor

vicb commented Feb 26, 2025

I think the doc should mention when this flag will be active by default with nodejs_compat - 2025-04-01 it seems

@@ -48,7 +56,11 @@ export default {
};
```

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.
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe expland "the cloudflare env object + passed the fetch handler, imported from cloudflare:workers or retrieved from an entry point"

Suggested change
the request `env` object. Doing so will cause you to lose any environment variables that
the cloudflare `env` object. Doing so will cause you to lose any environment variables that

@jasnell jasnell requested a review from a team as a code owner April 24, 2025 17:19
Copy link
Contributor

hyperlint-ai bot commented Apr 24, 2025

Howdy and thanks for contributing to our repo. The Cloudflare team reviews new, external PRs within two (2) weeks. If it's been two weeks or longer without any movement, please tag the PR Assignees in a comment.

We review internal PRs within 1 week. If it's something urgent or has been sitting without a comment, start a thread in the Developer Docs space internally.


PR Change Summary

Documented the handling of process.env and compatibility flags for Node.js in the Workers documentation.

  • Added a new section on populating process.env from environment variables using compatibility flags.
  • Updated the environment variables documentation to clarify how Node.js compatibility flags affect variable accessibility.
  • Enhanced the Node.js process documentation to explain the relationship between environment variables and process.env.

Modified Files

  • src/content/docs/workers/configuration/environment-variables.mdx
  • src/content/docs/workers/runtime-apis/nodejs/process.mdx

Added Files

  • src/content/compatibility-flags/process-env.md

How can I customize these reviews?

Check out the Hyperlint AI Reviewer docs for more information on how to customize the review.

If you just want to ignore it on this PR, you can add the hyperlint-ignore label to the PR. Future changes won't trigger a Hyperlint review.

Note specifically for link checks, we only check the first 30 links in a file and we cache the results for several hours (for instance, if you just added a page, you might experience this). Our recommendation is to add hyperlint-ignore to the PR to ignore the link check for this PR.

Co-authored-by: Brendan Irvine-Broque <[email protected]>
@jasnell jasnell requested review from vicb and irvinebroque April 24, 2025 17:30
list: never

name: "Populate process.env from environment variables"
sort_date: "2025-01-31"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort_date is usually the same as enable_date
Is there any reason to have a difference here?

Suggested change
sort_date: "2025-01-31"
sort_date: "2025-04-01"

disable_flag: "nodejs_compat_do_not_populate_process_env"
---

When the `nodejs_compat_populate_process_env` compatibility flag is used in conjunction with
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding is that this also depends on disallow_importable_env in the latest impl.

ref: cloudflare/workerd#3778

@@ -104,6 +104,47 @@ Select the **Secret** type if your environment variable is a [secret](/workers/c

<Render file="env_and_secrets" />

## Environment variables and Node.js compatibility

When you enable both the [`nodejs_compat`](/workers/runtime-apis/nodejs/) and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as previously


Text variable values are exposed directly.

JSON variable values that evaluate to string values are exposed as the parsed value.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember that there were changes wrt .toJson(), does the description here need to be updated.

The values of `process.env.FOO` and `process.env.BAR` will each be the
JavaScript string `"abc"`.

The value of `process.env.BAZ` will be the JSON-encoded string `"{ "a": 123 }"`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The value of `process.env.BAZ` will be the JSON-encoded string `"{ "a": 123 }"`.
The value of `process.env.BAZ` will be the JSON-encoded string `"{ \"a\": 123 }"`.

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.
Workers have a concept of [environment variables](/workers/configuration/environment-variables/) that are applied on a per-Worker and per-request basis.

By default, these are not accessible automatically via the `process.env` API. It is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
By default, these are not accessible automatically via the `process.env` API. It is
By default, these are not accessible via the `process.env` API. It is


By default, these are not accessible automatically via the `process.env` API. It is
possible to have environment variables and secrets automatically populated into `process.env` using
the `nodejs_compat_populate_process_env` compatibility flag. It is also possible to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, does this depend on the disallow_importable_env flag?

Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to:

  • handle all of the comments - meaning they don't have to be accepted but at least resolved
  • describe the interactions with disallow_importable_env

I would love to see some documentation for disallow_importable_env it might be in the current PR as they are closely related. If not it would be nice to have a tracking issue and make sure this gets documented.

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
product:workers Related to Workers product size/s
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants