Skip to content

Fix --require scripts missing env vars in cluster mode#6121

Open
tlhunter wants to merge 1 commit into
Unitech:masterfrom
tlhunter:fix/require-cluster-env-vars
Open

Fix --require scripts missing env vars in cluster mode#6121
tlhunter wants to merge 1 commit into
Unitech:masterfrom
tlhunter:fix/require-cluster-env-vars

Conversation

@tlhunter

@tlhunter tlhunter commented Jun 22, 2026

Copy link
Copy Markdown

This allows scripts that run via --require to access the intended ENV vars supplied by PM2.

We had a user-submitted issue that the Datadog tracer wouldn't properly read ENV var configuration when used with PM2. While we can fix this by having Datadog do the pm2_env parsing, it does seem like similar functionality would need to land in other libraries as well. For that reason it seems better to try and solve it inside of pm2.

In cluster mode, PM2 serialises the configured environment into a JSON
string passed as process.env.pm2_env, which ProcessContainer.js unpacks
at startup.  Node.js processes execArgv (including --require flags) before
the entry script runs, so any library loaded via --require saw the raw JSON
blob rather than the individual env vars.

Adds ProcessContainerClusterInitEnv.js, a tiny --require script that
expands pm2_env JSON into process.env.  ClusterMode.js now prepends
--require <this script> at the front of execArgv so the env is fully
expanded before any user-specified --require modules run.

Adds programmatic tests verifying that a --require script can read a
PM2-configured env var in both cluster mode (the regression) and fork mode.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@tlhunter tlhunter marked this pull request as draft June 22, 2026 22:25
@CLAassistant

CLAassistant commented Jun 22, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants