cli icon indicating copy to clipboard operation
cli copied to clipboard

`--prefix` flag ignores `overrides` from target package.json

Open tinovyatkin opened this issue 3 weeks ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current Behavior

When using npm install <package> --prefix <target-dir> from a different directory, npm correctly:

  • Reads <target-dir>/package.json
  • Writes dependencies to <target-dir>/package.json
  • Installs packages to <target-dir>/node_modules

However, npm ignores the overrides section from <target-dir>/package.json.

Expected Behavior

If --prefix makes npm use <target-dir>/package.json for reading dependencies and writing new ones, it should also respect the overrides section from that same file.

Steps To Reproduce

  1. Create a test directory structure:
mkdir -p /tmp/npm-prefix-test/target-dir
mkdir -p /tmp/npm-prefix-test/other-dir
  1. Create target-dir/package.json with an override:
{
  "name": "override-test",
  "version": "1.0.0",
  "overrides": {
    "ms": "2.0.0"
  }
}
  1. From other-dir, install a package that depends on ms:
cd /tmp/npm-prefix-test/other-dir
npm install [email protected] --prefix /tmp/npm-prefix-test/target-dir
  1. Check which version of ms was installed:
cat /tmp/npm-prefix-test/target-dir/node_modules/ms/package.json | grep version

Result: "version": "2.1.2" (or similar) - override was ignored

  1. Control test - run from target directory:
cd /tmp/npm-prefix-test/target-dir
rm -rf node_modules package-lock.json
npm install [email protected]
cat node_modules/ms/package.json | grep version

Result: "version": "2.0.0" - override was respected

Environment

  • npm: 10.9.2
  • Node.js: 22.12.0
  • OS: macOS Darwin 25.1.0

Source Code Analysis

Traced through the source code:

  1. lib/commands/install.js creates Arborist with path: npm.prefix
  2. lib/npm.js returns config.localPrefix for local installs
  3. workspaces/config/lib/index.js - loadLocalPrefix() correctly sets prefix from --prefix CLI flag
  4. workspaces/arborist/lib/arborist/build-ideal-tree.js - #initTree() reads package.json via PackageJson.normalize(this.path)
  5. workspaces/arborist/lib/node.js - loads overrides when loadOverrides: true

The path is correctly propagated, but somewhere in the chain the overrides from the target package.json are not being applied to the dependency resolution.

Workaround

The only reliable workaround is to cd into the target directory:

(cd /path/to/target && npm install <package>)

tinovyatkin avatar Dec 08 '25 08:12 tinovyatkin