npm / npm/cli

[BUG] Global install fails to add dependencies to `$PATH` for preinstall

Open
#6,080 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Needs Triage Release 8.x
Dominant language
JavaScript
Stars
10.1k
Forks
4.7k
Avg merge
2d 2h
Merged PRs (30d)
19

Description

Is there an existing issue for this?
  • I have searched the existing issues
This issue exists in the latest npm version
  • I am using the latest npm
Current Behavior

In a minimalistic package.json file with the following preinstall step, global installation (npm i -g) fails with "mkdirp: command not found":

    "preinstall": "mkdirp ~/some-dir ~/some-other-dir",

mkdirp is in the dependencies section and so replacing mkdirp with npm exec mkdirp makes no difference. Locally installing the project works fine though.

Observe that this is an issue in NPM 8 as well! A pure global installation works neither with NPM 8 nor 9, but with NPM 8 it will work if you work around it by first installing it locally (npm i; npm i -g;).

Expected Behavior

I would expect to have a global install work in just the same way as a local install; installing the listed dependencies and putting them on the $PATH for the NPM scripts.

git clone https://github.com/fatso83/issue-reproductions 
cd issue-reproductions/npm-bug
npm i -g # should not fail with command not found

Of course, the dummy repro production has no content and makes no sense to install globally, but it shows the issue clearly.

Steps To Reproduce

I have a repro project you can check out with the same commands executed in 5 different ways (NPM 8/9 with/without local install before global install).

Given NPM 9, this should be sufficient to show it:

git clone https://github.com/fatso83/issue-reproductions 
cd issue-reproductions/npm-bug
npm i -g # should not fail with command not found

Otherwise, creating a new project and adding preinstall step should suffice as well:

  1. Using NPM 9
  2. mkdir npm-test && cd npm-test && npm init -y && npm i --save mkdirp
  3. Add "preinstall": "mkdirp some-dir", to "scripts" section.
  4. npm install -g
Environment
  • npm: 9.2
  • Node.js: 19.4
  • OS Name: macOS 13.2
  • System Model Name: MacBook Air (m1)
  • npm config:
; "user" config from /Users/carlerik/.npmrc

//registry.npmjs.org/:_authToken = (protected)

; "cli" config from command line options

location = "project"

; node bin location = /usr/local/bin/node
; node version = v19.4.0
; npm local prefix = /Users/carlerik/dev/issue-reproductions/npm-bug
; npm version = 9.2.0
; cwd = /Users/carlerik/dev/issue-reproductions/npm-bug
; HOME = /Users/carlerik
; Run `npm config ls -l` to show all defaults.
$ npx envinfo --system

  System:
    OS: macOS 13.2
    CPU: (8) arm64 Apple M1
    Memory: 134.14 MB / 8.00 GB
    Shell: 3.2.57 - /bin/bash

PATH

/opt/homebrew/Cellar/postgresql@12/12.9_1/bin:/Users/carlerik/.gem/ruby/2.7.0/bin:/Users/carlerik/.gem/ruby/2.7.0/bin:/usr/local/bin:/opt/google-cloud-sdk/bin:/opt/homebrew/opt/php@7.4/bin:/opt/homebrew/opt/ruby/bin:/opt/homebrew/lib/ruby/gems/3.0.0/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/carlerik/Library/Python/3.9/bin:/Users/carlerik/Library/Python/3.7/bin:/usr/local/opt/php@7.2/sbin:/usr/local/opt/postgresql@10/bin:/usr/local/opt/openjdk/bin:/Users/carlerik/.sdkman/candidates/maven/current/bin:/Users/carlerik/.sdkman/candidates/java/current/bin:/Users/carlerik/.symfony/bin:/Users/carlerik/.yarn/bin:/Users/carlerik/.config/yarn/global/node_modules/.bin:/Users/carlerik/go/bin:/Users/carlerik/.go/bin:/Users/carlerik/.cargo/bin:/Users/carlerik/.local/bin:/Users/carlerik/.rvm/gems/ruby-2.7.2/bin:/Users/carlerik/.rvm/gems/ruby-2.7.2@global/bin:/Users/carlerik/.rvm/rubies/ruby-2.7.2/bin:/Users/carlerik/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion Tech Preview.app/Contents/Public:/Library/Apple/usr/bin:/Users/carlerik/.rvm/bin

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Clone https://github.com/fatso83/issue-reproductions and enter npm-bug, then run the documented npm 9 global-install reproduction and compare it with the local-install cases on npm 8 and 9. Done means a global install runs the preinstall script with dependency commands such as mkdirp available on PATH without the command-not-found failure.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
cli, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.