# Essential npm CLI Commands

> Beyond simply installing packages, the npm CLI provides a powerful toolkit for debugging dependency trees, upgrading outdated packages, and optimizing your `node_modules` structure.

Subject: Package Managers | Lesson 23 of 24 | 5 min read
Interactive lesson: https://codewithanimation.in/package-managers/npm-cli-toolkit
Previous: [Deterministic CI Builds & Cache Optimization](https://codewithanimation.in/package-managers/npm-ci-and-ci-optimization.md) | Next: [Dead Dependencies & Bundle Weight](https://codewithanimation.in/package-managers/depcheck-and-bundle-size.md)

## What is it?

Commands like `npm ls` and `npm explain` allow you to interrogate your dependency graph. If multiple versions of a package are installed, these tools help you understand *why* they were pulled in.

Commands like `npm dedupe` and `npm prune` actively clean and optimize the tree.

## Why use it?

When you encounter a 'peer dependency conflict' or wonder why a specific vulnerability is flagging in your project, you need visibility into the transitive tree. Searching through `package-lock.json` manually is tedious and error-prone.

## How it works

npm outdated compares installed versions with wanted versions satisfying the manifest and versions tagged latest in the registry. The latest tag is not necessarily the numerically greatest release.

Use `npm explain <package-name>` to trace the path from your root project down to a nested transitive dependency.

Use `npm link` when developing a library locally, allowing you to test it in another project without publishing to a registry.

## Common mistake: Duplicate Dependencies

Different compatible placement choices can duplicate installed packages, while incompatible version requirements may legitimately need multiple copies. npm dedupe attempts sharing where resolution permits; installed duplication is not itself a measurement of the browser bundle.

## Tip: Checking Extraneous Packages

If you remove a package from `package.json` without running an uninstall command, it stays in `node_modules`. Running `npm prune` deletes these extraneous, unlisted packages.

## Try it: Interactive Demo

Explore the concept in action using the interactive visualization below.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/package-managers/npm-cli-toolkit

## Check yourself

### Question 1

Which command explains why a particular installed package is in your tree?

- A. npm init -y
- B. npm run build
- C. npm explain <package>
- D. npm publish

Correct answer: C

explain traces dependency relationships that caused installation. Use npm ls --all when you want the full logical installed dependency tree.

- Why not A: init creates a manifest; it does not trace an installed dependency.
- Why not B: A build script is not the dependency explanation command.
- Why not D: Publication does not explain why a local package was installed.

### Question 2

In npm outdated, how do wanted and latest differ?

- A. Both always mean the numerically greatest release
- B. wanted is the installed version; latest is the lockfile version
- C. latest can never be outside your declared range
- D. wanted satisfies the declared range; latest follows the registry's latest tag

Correct answer: D

The latest tag need not be the numerically greatest version. wanted expresses the highest matching version for the declared range in this context.

- Why not A: A registry tag and a declared range can select different versions.
- Why not B: Installed current and locked resolutions are different concepts.
- Why not C: The latest-tagged version may exceed that range.

### Question 3

Which comparison between npm dedupe and npm prune is correct?

- A. dedupe attempts compatible sharing; prune removes extraneous installed packages
- B. dedupe removes every direct dependency; prune adds missing ones
- C. Both guarantee that only one version of each package remains
- D. Both compile the application bundle

Correct answer: A

Dedupe simplifies eligible dependency placement. Prune removes packages not required by the dependency lists, with omission flags able to affect more categories.

- Why not B: Neither description matches these commands.
- Why not C: Conflicting requirements may need multiple versions.
- Why not D: They manage the installed dependency tree, not application compilation.

## Remember this

- npm ls shows the installed logical tree at its selected depth; use --all for all dependency levels, and npm explain to trace why a package is present.
- `npm outdated` shows which packages have updates available.
- `npm dedupe` flattens the tree to remove duplicate installations of the same version.
- `npm prune` cleans up unlisted packages from `node_modules`.
