npm / npm/cli

[BUG] Inconsistent behavior of `--omit=optional` in `npm i` and `npm ls`

Open
#9,962 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Needs Triage
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
This is not just a request to bump a dependency for a CVE
  • This is not solely a request to bump a dependency for a CVE
Current Behavior

npm ls --omit=optional --all continues to include optional dependencies in its output.

However, npm ls --omit=dev --all excludes all development-only dependencies.

Additionally, npm i --omit=optional excludes all optional dependencies from being installed (docs).

Expected Behavior

When --omit=optional is supplied to npm ls, optional dependency entries should be excluded from the reported dependency tree, just like --omit=dev consistently excludes development-only dependency roots from the output and npm i --omit=optional does not install optional dependencies.

Steps To Reproduce
  1. Create a project with a package.json like this:
{
  "name": "npm-omit-optional-reproduction",
  "private": true,
  "version": "1.0.0",
  "dependencies": {
    "@stencil/core": "4.39.0"
  }
}
  1. Install dependencies:
npm install
  1. Then run:
npm ls --omit=optional --alll

The output includes optional dependencies:

└─┬ @stencil/core@4.39.0
  ├── @rollup/rollup-darwin-arm64@4.34.9
  ├── UNMET OPTIONAL DEPENDENCY @rollup/rollup-darwin-x64@4.34.9
  ├── UNMET OPTIONAL DEPENDENCY @rollup/rollup-linux-arm64-gnu@4.34.9
  ├── UNMET OPTIONAL DEPENDENCY @rollup/rollup-linux-arm64-musl@4.34.9
  ├── UNMET OPTIONAL DEPENDENCY @rollup/rollup-linux-x64-gnu@4.34.9
  ├── UNMET OPTIONAL DEPENDENCY @rollup/rollup-linux-x64-musl@4.34.9
  ├── UNMET OPTIONAL DEPENDENCY @rollup/rollup-win32-arm64-msvc@4.34.9
  └── UNMET OPTIONAL DEPENDENCY @rollup/rollup-win32-x64-msvc@4.34.9

The same behavior is visible in JSON output:

npm ls --json --all --omit=optional
  1. Then run a clean re-install with the --omit=optional flag set:
npm ci --omit=optional
  1. Finally execute ls (--omit=all is not needed):
npm ls --omit=optional --all

The output doesn't include any optional dependency:

└──  @stencil/core@4.39.0
Environment
  • npm: 12.0.1
  • Node.js: v24.18.0
  • OS Name: macOS arm64
  • 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

Start with the package.json reproduction and compare npm ls --json --all --omit=optional before and after npm ci --omit=optional. Trace the npm ls handling of the --omit=optional flag, then verify that optional entries are absent from both regular and JSON dependency-tree output while --omit=dev behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
cli, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.