Closed
Description
Current Behavior:
npm install
runs in a temp cache directory (somewhere in Users/user/.npm/_cacache/tmp/my_dep/
) when installing dependencies on macOS npm 7.10.0. This causes errors when your pre/post-install scripts relies on checking for certain files/folders in the directory the command was ran in. However, on Windows, it does run them in the current directory.
Expected Behavior:
npm install
should run the scripts in the directory that it was ran in, like Windows like it was on npm 6, or provide some way to retrieve this directory. Note that the INIT_CWD
and other env variables do not point to this directory, but rather the cache directory.
Steps To Reproduce:
- Use npm 7.10.0
- Create a package with the following
scripts
section
"scripts": {
"preinstall": "node pre.js",
"postinstall": "node post.js"
}
- Create a preinstall script,
pre.js
, with the contents
const fs = require('fs');
const path = require('path');
// On ~~macOS~~ npm v7+ this points to npm's temp cache directory
console.log(process.env.INIT_CWD);
// my_folder is a folder that exists in the directory of "npm install".
// This works on ~~Windows~~ npm v6, but it errors on ~~macOS~~ npm v7 because this folder does not exist in npm's cache directory
const files = fs.readdirSync(path.join(process.env.INIT_CWD, 'my_folder'));
- Publish or upload this package to github
- Create a test folder and a package.json. Add the dependency to the package.json
- Run
npm install
. - Preinstall script fails
Environment:
macOS:
- macOS (M1 chip) Big Sur 11.2.3
- Node: 16.0.0
- npm: 7.10.0
Windows:
- Windows 10 Pro, version 20H2, build 19042.928
- Node: 14.12.0
- Npm: 6.14.13, it errors when using npm 7.10.0