npm / npm/cli

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

Open
#8,835 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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
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 debug@4.3.4 --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 debug@4.3.4
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>)

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

Reproduce the issue with the documented --prefix commands, then trace lib/commands/install.js, lib/npm.js, workspaces/config/lib/index.js, workspaces/arborist/lib/arborist/build-ideal-tree.js, and workspaces/arborist/lib/node.js. Determine where the target package.json overrides stop being applied and add coverage showing that --prefix installs the overridden dependency version.

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
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.