363

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.

BiAiB
  • 12,932
  • 10
  • 43
  • 63
  • 9
    If all you're after is a dependency graph, http://npm.broofa.com and https://npm.anvaka.com will do this for publicly published modules. – broofa Aug 23 '20 at 06:05
  • 1
    @broofa thanks for those links ! I suggest you post them as answers. I checked both on @angular/cli and npm.anvaka.com is sluggish and unreadable, but npm.broofa.com does a great job out of it – BiAiB Aug 25 '20 at 07:11

12 Answers12

397

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

npm ls --all

This will generate a dependency tree for the project at the current directory and print it to the console. (The all option shows all transitive dependencies, not just those directly depended upon by the current project - see the documentation.)

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

npm ls [dependency]

You can also set the maximum depth level by doing

npm ls --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.

Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
mgthomas99
  • 5,402
  • 3
  • 19
  • 21
154

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, npm@5.2.0 installed then you can use npx and avoid globally installing the command - just call:

npx npm-remote-ls bower
JustCarty
  • 3,839
  • 5
  • 31
  • 51
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
125

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!

Stan Bondi
  • 4,118
  • 3
  • 24
  • 35
56

Here is the unpowerful official command:

npm view <PACKAGE> dependencies

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

golopot
  • 10,726
  • 6
  • 37
  • 51
50

You can use howfat which also displays dependency statistics:

npx howfat jasmine

screensot

Alexey Prokhorov
  • 3,431
  • 1
  • 22
  • 25
26

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 "mime-db@1.37.0"
    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.

msangel
  • 9,895
  • 3
  • 50
  • 69
21

This command output all modules with dependencies in a tree structure:

npm ls -a
alanextar
  • 1,094
  • 13
  • 16
19

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
Community
  • 1
  • 1
Yogendra Singh
  • 2,063
  • 25
  • 20
9

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
pipegrepper
  • 324
  • 3
  • 11
5

To get it as a list:

% npx npm-remote-ls --flatten dugite -d false -o false
[
  'dugite@1.91.3',
  'checksum@0.1.1',
  'progress@2.0.3',
  'mkdirp@0.5.5',
  'rimraf@2.7.1',
  'tar@4.4.13',
  'optimist@0.3.7',
  'got@9.6.0',
  'minimist@1.2.5',
  'chownr@1.1.4',
  'glob@7.1.6',
  'fs-minipass@1.2.7',
  'minizlib@1.3.3',
  'minipass@2.9.0',
  'safe-buffer@5.2.1',
  'yallist@3.1.1',
  'wordwrap@0.0.3',
  '@szmarczak/http-timer@1.1.2',
  'cacheable-request@6.1.0',
  '@sindresorhus/is@0.14.0',
  'decompress-response@3.3.0',
  'duplexer3@0.1.4',
  'lowercase-keys@1.0.1',
  'mimic-response@1.0.1',
  'get-stream@4.1.0',
  'to-readable-stream@1.0.0',
  'p-cancelable@1.1.0',
  'url-parse-lax@3.0.0',
  'fs.realpath@1.0.0',
  'inflight@1.0.6',
  'inherits@2.0.4',
  'once@1.4.0',
  'path-is-absolute@1.0.1',
  'minimatch@3.0.4',
  'defer-to-connect@1.1.3',
  'clone-response@1.0.2',
  'get-stream@5.2.0',
  'http-cache-semantics@4.1.0',
  'lowercase-keys@2.0.0',
  'responselike@1.0.2',
  'keyv@3.1.0',
  'pump@3.0.0',
  'prepend-http@2.0.0',
  'normalize-url@4.5.0',
  'wrappy@1.0.2',
  'brace-expansion@1.1.11',
  'json-buffer@3.0.0',
  'end-of-stream@1.4.4',
  'concat-map@0.0.1',
  'balanced-match@1.0.0'
]
林东吴
  • 171
  • 1
  • 8
4

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

For example:

https://bundlephobia.com/result?p=sanitize-html@1.19.1

enter image description here

zubko
  • 1,718
  • 25
  • 28
1

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/router@4.4.7 dependencies

yarn info @angular/router@4.4.7 peerDependencies

Robert Brisita
  • 5,461
  • 3
  • 36
  • 35
  • 2
    Why does yarn provide and accurate way to view dependencies and npm provides NO way to view dependencies, could this be because npm is not deterministic and yarn is? – Brian Ogden Feb 15 '20 at 02:53