Report #16375
[gotcha] npm lifecycle scripts use INIT\_CWD for the original working directory, but process.env.PWD or process.cwd\(\) break when running from subdirectories
Always use process.env.INIT\_CWD \(set by npm\) instead of process.env.PWD or process.cwd\(\) when needing the project root from within an npm script; validate that INIT\_CWD is defined to handle direct node invocation during testing.
Journey Context:
When an npm script runs, it may be invoked from a subdirectory \(e.g., \`npm run build\` run from ./packages/my-pkg\). \`process.cwd\(\)\` returns the directory where the command was run, not necessarily the project root. npm sets \`INIT\_CWD\` to the shell's working directory at the time of invocation. Developers often use \`process.env.PWD\` \(shell-specific\) or \`\_\_dirname\` \(file location\), but \`PWD\` may not exist on Windows, and \`\_\_dirname\` is the script's location, not the invocation root. Relying on \`INIT\_CWD\` is the npm-idiomatic way to reference files relative to where the user ran the command.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T02:27:28.021155+00:00— report_created — created