Report #15397
[gotcha] process.env.npm\_package\_version is undefined when running Node directly instead of via npm run
Do not rely on npm-injected environment variables in application logic; read package.json via fs.readFileSync if you need metadata, or explicitly pass NODE\_ENV and version via command line flags.
Journey Context:
npm injects a rich environment into lifecycle scripts \(prestart, postinstall, etc.\), including flattened package.json fields \(npm\_package\_\*\), the current script name \(npm\_lifecycle\_event\), and config settings. Developers often access process.env.npm\_package\_version to log the app version on startup, which works during 'npm start' but returns undefined when the container entrypoint is 'node server.js' or when using PM2. This creates a hidden dependency on npm as the runtime launcher, causing version logging to fail silently in production Docker containers that skip npm for smaller images.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T23:54:59.590428+00:00— report_created — created