npm / npm/cli

[BUG] Global installation / reinstallation of git packages fails if certain npm scripts are present

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

Nobody has claimed this yet.

Bug Priority 2 Release 9.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

There appear to be various issues with global installation of packages from git that have build / install / preinstall / postinstall / prepack / prepare scripts. Local installation appears to work as expected.

  1. If any of the following scripts are present, the global installation fails with an ENOENT error:

    • install
    • preinstall
    • postinstall
  2. If any of the following scripts are present, the first global installation results in the package being symlinked to the NPM cache directory which is ephemeral:

    • build
    • prepack
    • prepare
  3. If any of the following scripts are present, a global reinstallation fails with an ENOTDIR error:

    • build
    • prepack
    • prepare

I've tried various combinations of --install-links and --ignore-scripts but neither fixes these issues (--install-links changes the error code for the reinstallation bug to ENOTEMPTY). I've raised a single Issue for all of these bugs because they seem to be related.

Expected Behavior

Global installation of git packages should work in the same way as local installation:

  • Installation should not fail regardless of which scripts are present in the package
  • Reinstallation should not fail regardless of which scripts are present in the package
  • The installed package directory should not be a symlink to the NPM cache
Steps To Reproduce

First bug (first installation failure)

  1. Create an NPM package in git (e.g. https://github.com/paulbrimicombe/npm-git-package) with an install / preinstall / postinstall script

  2. Verify that local installation works correctly:

    # All of these should run successfully and the code will have been copied to node_modules/npm-git-package
    npm install paulbrimicombe/npm-git-package#with-install-script
    npm install paulbrimicombe/npm-git-package#with-preinstall-script
    npm install paulbrimicombe/npm-git-package#with-postinstall-script
    
  3. Attempt global installation - all of these will fail with ENOENT for the target global installation path for the package, i.e. {GLOBAL_NPM_PATH}/lib/node_modules/npm-git-package:

    npm install --global paulbrimicombe/npm-git-package#with-install-script
    npm install --global paulbrimicombe/npm-git-package#with-preinstall-script
    npm install --global paulbrimicombe/npm-git-package#with-postinstall-script
    

Second bug (installation is symlinked to NPM cache)

  1. Create an NPM package in git (e.g. https://github.com/paulbrimicombe/npm-git-package) with a build / prepack / prepare script

  2. Verify that local installation works correctly:

    # All of these should run successfully and the code will have been copied to node_modules/npm-git-package
    npm install paulbrimicombe/npm-git-package#with-build-script
    npm install paulbrimicombe/npm-git-package#with-prepack-script
    npm install paulbrimicombe/npm-git-package#with-prepare-script
    
  3. Install globally - all of these will complete successfully but {GLOBAL_NPM_PATH}/lib/node_modules/npm-git-package will be a symlink to the ephemeral NPM cache:

    npm install --global paulbrimicombe/npm-git-package#with-build-script
    npm install --global paulbrimicombe/npm-git-package#with-prepack-script
    npm install --global paulbrimicombe/npm-git-package#with-prepare-script
    

Third bug (cannot globally reinstall packages with certain scripts)

  1. Create an NPM package in git (e.g. https://github.com/paulbrimicombe/npm-git-package) with a build / prepack / prepare script

  2. Verify that local installation works correctly:

    # All of these should run successfully and the code will have been copied to node_modules/npm-git-package
    npm install paulbrimicombe/npm-git-package#with-build-script
    npm install paulbrimicombe/npm-git-package#with-prepack-script
    npm install paulbrimicombe/npm-git-package#with-prepare-script
    
  3. Install globally once (these will all pass):

    npm install --global paulbrimicombe/npm-git-package#with-build-script
    npm install --global paulbrimicombe/npm-git-package#with-prepack-script
    npm install --global paulbrimicombe/npm-git-package#with-prepare-script
    
  4. Attempt reinstallation — these will all fail with an ENOTDIR error:

    npm install --global paulbrimicombe/npm-git-package#with-build-script
    npm install --global paulbrimicombe/npm-git-package#with-prepack-script
    npm install --global paulbrimicombe/npm-git-package#with-prepare-script
    
Environment
  • npm: 9.9.1 (also tested on 8.x and had the same issue)
  • Node.js: 18.18.2 (also tested on 16 and had the same issue)
  • OS Name: Darwin Kernel 23 (MacOS)
  • npm config (since this is a global installation problem, I've included --global):
; "cli" config from command line options
global = true 

; node bin location = /Users/XXXXX/.local/share/nvm/v18.18.2/bin/node
; node version = v18.18.2
; npm local prefix = /Users/XXXXX/Development/toys/npm-install-tests
; npm version = 9.9.1
; cwd = /Users/XXXXX/Development/toys/npm-install-tests
; HOME = /Users/XXXXX
; Run `npm config ls -l` to show all defaults.
; copy and paste output from `npm config ls` here

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

Start with the reproduction commands and the npm-git-package fixture branches named in the issue, comparing local and global installs. Trace the global git-package installation and reinstallation paths, then verify behavior for install, preinstall, postinstall, build, prepack, and prepare scripts. Done means global installation and reinstallation succeed and the installed package is not a symlink to the npm cache.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.