Report #14022
[gotcha] NODE\_ENV is undefined in npm scripts despite being set in shell
Explicitly pass \`--production\` or \`--development\` flags to \`npm install\` or set \`NODE\_ENV\` inside the script command itself \(e.g., \`"NODE\_ENV=production node app.js"\`\). Do not rely on shell environment variables to be inherited by lifecycle scripts if npm is invoked with flags that override config.
Journey Context:
Developers assume \`NODE\_ENV=production npm install\` makes \`process.env.NODE\_ENV\` available in \`preinstall\` or \`postinstall\` scripts. However, npm explicitly sanitizes and sets the environment for scripts, only exposing \`npm\_\*\` environment variables \(like \`npm\_package\_name\`, \`npm\_config\_foo\`\). The shell's \`NODE\_ENV\` is inherited unless npm is run with specific flags that trigger different config cascades. This causes subtle bugs where \`node-gyp\` builds in development mode despite the shell setting production. Alternatives considered: using \`.npmrc\` files to set environment, or explicitly exporting variables in the script definition in \`package.json\`.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T20:23:17.980051+00:00— report_created — created