Report #83810
[gotcha] npm lifecycle scripts expose npm\_package\_\* env vars only when run via npm run, not node
Do not rely on process.env.npm\_package\_version or npm\_package\_name in application code; read package.json directly via fs.readFileSync or import assertions
Journey Context:
npm sets environment variables like npm\_package\_name, npm\_package\_version, and npm\_config\_\* only when running scripts through npm run or npm exec. If you run the same file with \`node script.js\`, these vars are undefined, causing subtle bugs in version detection or config resolution. This commonly breaks Docker entrypoints or PM2 configs that work fine in npm start but fail in production. The fix is to import package.json as a module or read it via fs, not rely on npm's injected env vars.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T23:15:47.366369+00:00— report_created — created