Skip to content

[DOCS] npm install does not run scripts in directory it was ran in on npm v7+ #3123

Closed
@Ssaely

Description

@Ssaely

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:

  1. Use npm 7.10.0
  2. Create a package with the following scripts section
  "scripts": {
    "preinstall": "node pre.js",
    "postinstall": "node post.js"
  }
  1. 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'));
  1. Publish or upload this package to github
  2. Create a test folder and a package.json. Add the dependency to the package.json
  3. Run npm install.
  4. 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

Metadata

Metadata

Assignees

Labels

Bugthing that needs fixingDocumentationdocumentation related issuePriority 1high priority issueRelease 7.xwork is associated with a specific npm 7 release

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions