How to view the dependency tree of a given npm module?

Npm

Npm Problem Overview


How can I get the tree of a module available to npm, but not installed locally ?

npm ll does the job for locally installed packages. But it doesn't work for modules not installed or modules installed globally.

I tried npm list bower but that's not it.

Npm Solutions


Solution 1 - Npm

You can generate NPM dependency trees without the need of installing a dependency by using the command

npm list

This will generate a dependency tree for the project at the current directory and print it to the console.

You can get the dependency tree of a specific dependency like so:

npm list [dependency]

You can also set the maximum depth level by doing

npm list --depth=[depth]

Note that you can only view the dependency tree of a dependency that you have installed either globally, or locally to the NPM project.

Solution 2 - Npm

You can use the npm-remote-ls module. You can install it globally:

npm install -g npm-remote-ls

And then call:

npm-remote-ls bower

Alternatively, [email protected] installed then you can use npx and avoid globally installing the command - just call:

npx npm-remote-ls bower

Solution 3 - Npm

This site allows you to view a packages tree as a node graph in 2D or 3D.

http://npm.anvaka.com/#/view/2d/waterline

enter image description here

Great work from @Avanka!

Solution 4 - Npm

Here is the unpowerful official command:

npm view <PACKAGE> dependencies

It prints only the direct dependencies, not the whole tree.

Solution 5 - Npm

You can use howfat which also displays dependency statistics:

npx howfat -r tree jasmine

screensot

Solution 6 - Npm

If you want to get the actually dependency path of specific package and want to know why you have it, you can simply ask yarn why <MODULE>. example:

    $> yarn why mime-db
    yarn why v1.5.1
    [1/4] Why do we have the module "mime-db"...?
    [2/4] Initialising dependency graph...
    [3/4] Finding dependency...
    [4/4] Calculating file sizes...
    => Found "[email protected]"
    info Reasons this module exists
       - "coveralls#request#mime-types" depends on it
       - Hoisted from "coveralls#request#mime-types#mime-db"
    info Disk size without dependencies: "196kB"
    info Disk size with unique dependencies: "196kB"
    info Disk size with transitive dependencies: "196kB"
    info Number of shared dependencies: 0
    Done in 0.65s.

Solution 7 - Npm

View All the metadata about npm module

npm view mongoose(module name)

View All Dependencies of module

npm view mongoose dependencies

View All Version or Versions module

npm view mongoose version
npm view mongoose versions

View All the keywords

npm view mongoose keywords

Solution 8 - Npm

Solution 9 - Npm

If you are using yarn, then you can go with yarn list from the root directory of the project. It'll give you a tree like structure of all the transitive dependencies like below:

├─ @ampproject/toolbox-core@2.7.4
│  ├─ cross-fetch@3.0.6
│  └─ lru-cache@6.0.0
├─ @ampproject/toolbox-optimizer@2.7.0-alpha.1
│  ├─ @ampproject/toolbox-core@^2.6.0
│  ├─ @ampproject/toolbox-runtime-version@^2.7.0-alpha.1
│  ├─ @ampproject/toolbox-script-csp@^2.5.4
│  ├─ @ampproject/toolbox-validator-rules@^2.5.4
│  ├─ abort-controller@3.0.0
│  ├─ cross-fetch@3.0.5
│  ├─ cross-fetch@3.0.5
│  │  └─ node-fetch@2.6.0
│  ├─ cssnano-preset-simple@1.2.0
│  │  ├─ caniuse-lite@^1.0.30001093
│  │  ├─ postcss@^7.0.32
│  │  └─ postcss@7.0.35
│  │     ├─ chalk@^2.4.2
│  │     ├─ source-map@^0.6.1
│  │     └─ supports-color@^6.1.0

Solution 10 - Npm

There is also a nice web app to see the dependencies in a weighted map kind of view.

For example:

https://bundlephobia.com/[email protected]

Solution 11 - Npm

Unfortunately npm still doesn't have a way to view dependencies of non-installed packages. Not even a package's page list the dependencies correctly. 

Luckily installing yarn:

brew install yarn

Allows one to use its info command to view accurate dependencies:

yarn info @angular/[email protected] dependencies

yarn info @angular/[email protected] peerDependencies

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBiAiBView Question on Stackoverflow
Solution 1 - Npmmgthomas99View Answer on Stackoverflow
Solution 2 - NpmGergo ErdosiView Answer on Stackoverflow
Solution 3 - NpmStan BondiView Answer on Stackoverflow
Solution 4 - NpmgolopotView Answer on Stackoverflow
Solution 5 - NpmAlexey ProkhorovView Answer on Stackoverflow
Solution 6 - NpmmsangelView Answer on Stackoverflow
Solution 7 - NpmYogendra SinghView Answer on Stackoverflow
Solution 8 - Npm林东吴View Answer on Stackoverflow
Solution 9 - NpmpipegrepperView Answer on Stackoverflow
Solution 10 - NpmzubkoView Answer on Stackoverflow
Solution 11 - NpmRobert BrisitaView Answer on Stackoverflow