-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
If you're using npm workspaces, npm appears to use the "last occurrence" of a given dependency as the final resolution for a package's "peer" status.
if you have the following three conditions met:
- workspaces config like this:
"workspaces": [
"apps/*",
"services/*"
]- a package in
appshas a dev dependency on a package with optional dependencies (in my case,lightningcssis the dev dep and it has optional dependencies for platform-specific lightningcss builds i.e.lightningcss-darwin-arm64) - a package in
serviceshas an optional peer dep on the same dependency (in my case,lightningcssthrough an optional peer dep fromvite)
NPM will mark the optional dependency (e.g. lightningcss-darwin-arm64) as an optional peer dev dependency, and due to the changes in #8431, will no longer install the dependency.
This only occurs if the optional peer dep occurs "last" - if you flip the order of workspaces (swap apps/* and services/*) then it's correctly flagged as an optional (non-peer) dependency. Also, if you make the dev dependency a prod dependency in either package, the behavior does not occur.
Expected Behavior
npm should consider all usages of a package in a workspaces repo before marking a certain dependency as "peer"/"optional" or not.
Steps To Reproduce
- add the files to a local directory from this gist
- run
npm install - run
npm why lightningcss-darwin-arm64 - notice that it's flagged as a "optional" "dev" "peer" dependency
- run
npm installagain - see that the
lightningcss-darwin-arm64package has been removed.
Environment
- npm: verified on latest v10 and latest v11
- Node.js: v24
- OS Name: macOS Sequoia
- System Model Name: n/a
- npm config: n/a
Apologies if this is a duplicate issue, I tried to find a matching issue but didn't find any that seemed like it fit the problem I was seeing.