npm / npm/cli

[BUG] npm ls throws invalid package error for optional dependency of a different version found in dependency tree

Open
#6,565 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Needs Triage 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

Consider this package json and package lock json setup :https://github.com/ashlymat/npmDummy

  • Clone the above repo
  • Run npm ci
  • Run npm ls --json --long --all with or without --omit=optional

I was able to reproduce with 8.19.1 npm locally and with docker image node:18 which has npm 9.5.1

Error seen:

npm ERR! code ELSPROBLEMS
npm ERR! invalid: commander@2.9.0 /Users/I560834/Dev/others/npmDummy/node_modules/commander
{
  "error": {
    "code": "ELSPROBLEMS",
    "summary": "invalid: commander@2.9.0 /Users/I560834/Dev/others/npmDummy/node_modules/commander",
    "detail": ""
  }
}

In the above repo, .npmrc has a setting of omit=optional

when npm ci is run , it respects this setting and does not install optional dependency for commander package
and hence there is nothing installed in node_modules/z-schema/node_modules/commander folder.

But when npm ls --json --long --all is run , somehow this optional setting is not respected and it complains about another version of the same commander package being installed in the parent node_modules folder.
This version in the parent folder is a dependecy from another package @sap/approuter itself.

The problem goes away if we remove omit=optional setting in .npmrc. This is because it then has the correct version installed in node_modules/z-schema/node_modules/commander folder.

The problem also doesn't exist if there was no other version of commander package at all in the node_modules folder.

Summarizing it,

npm ls --all seems to have an issue in omitting optional dependencies when another version of the dependency is present in node_modules.

Expected Behavior

It seems like npm ls should have a check to identify if the optional dependency of a different version in the dependency tree - is required by some other package in the tree, before throwing an error.

Steps To Reproduce

Have been described above

Environment
  • npm: 8.9.1 and 9.5.1
  • Node.js: node: 18 docker image
  • OS Name:
  • System Model Name:
  • npm config:
; 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

Clone the linked npmDummy repository, run npm ci, then compare npm ls --json --long --all with and without --omit=optional. Trace the npm CLI dependency-tree inspection that reports commander@2.9.0 as invalid when the optional nested dependency is omitted. Done means the reproduced setup no longer reports this optional dependency as invalid while preserving validation of genuinely required dependencies.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
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.