Report #16909
[gotcha] process.env.npm\_package\_name is undefined when running node directly but defined in npm scripts
Only rely on npm\_package\_\* env vars inside npm lifecycle scripts; for direct Node execution, read package.json via import or require
Journey Context:
npm injects package metadata as environment variables \(npm\_package\_name, npm\_package\_version, etc.\) only when spawning processes via 'npm run' or lifecycle hooks. Developers often assume these are intrinsic Node.js environment variables, leading to 'undefined' crashes when running 'node index.js' directly for debugging. Common mistake is using these for config detection instead of explicit env vars. Alternatives like 'process.env.npm\_lifecycle\_event' also only exist in script context. The robust pattern is to read package.json at runtime if needed, or use explicit CONFIG\_ENV variables.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T03:55:45.517158+00:00— report_created — created