cli
cli copied to clipboard
`--prefix` flag ignores `overrides` from target package.json
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
- Create a test directory structure:
mkdir -p /tmp/npm-prefix-test/target-dir
mkdir -p /tmp/npm-prefix-test/other-dir
- Create
target-dir/package.jsonwith an override:
{
"name": "override-test",
"version": "1.0.0",
"overrides": {
"ms": "2.0.0"
}
}
- From
other-dir, install a package that depends onms:
cd /tmp/npm-prefix-test/other-dir
npm install [email protected] --prefix /tmp/npm-prefix-test/target-dir
- Check which version of
mswas 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
- 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:
lib/commands/install.jscreates Arborist withpath: npm.prefixlib/npm.jsreturnsconfig.localPrefixfor local installsworkspaces/config/lib/index.js-loadLocalPrefix()correctly sets prefix from--prefixCLI flagworkspaces/arborist/lib/arborist/build-ideal-tree.js-#initTree()reads package.json viaPackageJson.normalize(this.path)workspaces/arborist/lib/node.js- loads overrides whenloadOverrides: 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>)