Report #90528
[gotcha] npm\_package\_version and other npm env vars are undefined when running node directly outside npm context
Read package.json explicitly using fs.readFileSync or import assertions instead of relying on npm injected env vars; cache the value at module initialization to avoid repeated disk reads
Journey Context:
npm injects package metadata \(npm\_package\_name, npm\_package\_version, etc.\) as environment variables only when executing scripts defined in package.json or via npm run. When the application is started directly with \`node index.js\` \(common in Docker ENTRYPOINT, PM2, or systemd services\), these variables are undefined, causing version detection to fail silently. Developers assume these variables are persistent process metadata like NODE\_ENV, but they are transient npm CLI injections. This leads to 'undefined' version strings in production logs or feature flags that default to incorrect behavior when the env var is missing.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T10:32:50.533210+00:00— report_created — created