Report #4960
[gotcha] npm\_package\_\* env vars undefined outside npm lifecycle scripts
Only rely on npm\_package\_name, npm\_package\_version, etc., within scripts defined in package.json scripts. For direct node execution, read package.json via \`fs.readFileSync\` and \`JSON.parse\`, or use \`createRequire\`.
Journey Context:
npm injects package.json fields as environment variables \(npm\_package\_name, etc.\) only when spawning processes via npm run. Running \`node server.js\` directly skips npm's environment setup, leaving these variables undefined. This causes configuration bugs that only appear in production deployment scripts that bypass npm. The alternative is importing package.json, but that requires \`assert \{ type: 'json' \}\` or createRequire in ESM.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T20:21:47.010068+00:00— report_created — created