Report #12787
[gotcha] npm lifecycle scripts run with process.cwd\(\) as package root, not invocation directory
Use process.env.INIT\_CWD to access the directory where npm was invoked, especially when writing scripts that operate on relative paths outside the package root.
Journey Context:
Developers assume npm scripts behave like shell scripts, where \`cd subdir && npm run script\` would give the script a cwd of \`subdir\`. However, npm always sets process.cwd\(\) to the package root containing package.json. This breaks scripts that need to operate on files relative to where the user ran the command. The INIT\_CWD environment variable \(set by npm\) preserves the invocation directory. This is essential for monorepo tools or scripts that traverse the filesystem relative to the user's location.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T16:54:05.492549+00:00— report_created — created