986

How do I list the user-installed / environment package only in npm?

When I do npm -g list, it outputs every package and their dependencies. Instead I'd like to see the packages installed in the current working project or environment.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lolski
  • 16,231
  • 7
  • 34
  • 49

19 Answers19

1884
npm list -g --depth=0
  • npm: the Node.js package manager command line tool
  • list -g: display a tree of every package found in the user’s folders (without the -g option it only shows the current directory’s packages)
  • --depth 0 / --depth=0: avoid including every package’s dependencies in the tree view
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
aris
  • 22,725
  • 1
  • 29
  • 33
  • 2
    Return empty result. My npm version is 3.6.0. – Bagusflyer Jul 21 '16 at 04:07
  • 20
    Why the `--depth=0` option is not mentioned when I run `npm help ls`? – Marecky Mar 07 '18 at 10:48
  • 1
    If you install your modules at another folder using `--prefix`, then you need to add the `--prefix` too into this `npm list` command. – Sany Liew Aug 06 '18 at 04:06
  • 3
    @Fabricio, you can run `npm config set depth 0` to make it the default. – David Y. Stephenson Sep 17 '18 at 12:15
  • It doesn't work for me on Windows: `C:\Web\git>npm list -g --depth=0 C:\Users\david\AppData\Roaming\npm `-- (empty)` – David Spector Jan 16 '19 at 21:11
  • I've installed several packages, but my npm directory is empty! Bug in npm? I got warning messages on each install: `C:\Web\git>npm install --save-dev webpack npm WARN saveError ENOENT: no such file or directory, open 'C:\Web\git\package.json' npm WARN enoent ENOENT: no such file or directory, open 'C:\Web\git\package.json' npm WARN git No description npm WARN git No repository field. npm WARN git No README data npm WARN git No license field. npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.6 (node_modules\fsevents): npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: ...` – David Spector Jan 16 '19 at 21:14
  • 3
    It also might be good to add `| grep -v "duped"` to remove duplicated dependencies onto the list – Spencer Pollock Feb 08 '19 at 21:23
  • The following is quite fast: `find "$(npm root -g)" -maxdepth "$(<<< "$(npm root -g)" sed 's:/: :g' | wc -w)" -name package.json | cut -d/ -f5- | sed -e 's:/package.json$::' -e '/^[^@].*\//d'`. But is it correct? – Mmmh mmh Mar 03 '21 at 19:50
  • @Marecky it's buried at the bottom. Look under the configuration section – lasec0203 Mar 19 '21 at 02:22
  • can i get How i had installed i mean the command like if i had installed using `npm i -g @angular/cli` then i want to get `@angular/cli` , for all packages that i've installed? – Rajanboy Jun 11 '22 at 05:39
  • To update all global packages run `npm update -g` to update single `npm update -g ` – Lukas Liesis Aug 07 '22 at 19:33
172

You can get a list of all globally installed modules using:

ls `npm root -g`
Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Gil
  • 3,529
  • 1
  • 19
  • 22
147

As of 13 December 2015

npm list illustration

While I found the accepted answer 100% correct, and useful, I wished to expand upon it a little based on my own experiences, and hopefully for the benefit of others too. (Here I am using the terms package and module interchangeably)

In an answer to the question, yes the accepted answer would be:

npm list -g --depth=0

You might wish to check for a particular module installed globally, on Unix-like systems or when grep is available. This is particularly useful when checking what version of a module you are using (globally installed; just remove the -g flag if checking a local module):

npm list -g --depth=0 | grep <module_name>

If you'd like to see all available (remote) versions for a particular module, then do:

npm view <module_name> versions

Note, versions is plural. This will give you the full listing of versions to choose from.

For the latest remote version:

npm view <module_name> version

Note, version is singular.

To find out which packages need to be updated, you can use:

npm outdated -g --depth=0

To update global packages, you can use

npm update -g <package>

To update all global packages, you can use:

npm update -g

(However, for npm versions less than 2.6.1, please also see this link as there is a special script that is recommended for globally updating all packages.)

The above commands should work across NPM versions 1.3.x, 1.4.x, 2.x and 3.x.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
arcseldon
  • 35,523
  • 17
  • 121
  • 125
  • 1
    To update a specific global package: `npm update -g `. I believe `update` is preferable to `install` here because npm will be smart and do the installation only if the specified package is outdated. – Robin A. Meade Jan 27 '17 at 22:22
  • 1
    @Talespin_Kit probably screentogif – forresthopkinsa Jul 19 '17 at 01:19
  • @Talespin_Kit ShareX is FANTASTIC for creating and sharing gif screenshots (and any other screen shots). –  Apr 10 '19 at 10:49
66

List NPM packages with some friendly GUI!

This is what I personally prefer and it may be for others too, it may also help during presentations or meetings.

With npm-gui you can list local and global packages with a better visualization.

You can find the package at

Run the following

// Once
npm install -g npm-gui

cd c:\your-prject-folder
npm-gui localhost:9000

Then open your browser at http:\\localhost:9000

npm-gui

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
30

For project dependencies use:

npm list --depth=0

For global dependencies use:

npm list -g --depth=0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Karthik damodara
  • 1,805
  • 19
  • 17
28

Use:

npm ls

npm list is just an alias for npm ls.

For the extended information, use:

npm la
npm ll

You can always set --depth=0 at the end to get the first level deep.

npm ls --depth=0

You can check development and production packages.

npm ls --only=dev
npm ls --only=prod

To show the info in json format

npm ls --json=true

The default is false

npm ls --json=false

You can insist on long format to show extended information.

npm ls --long=true

You can show parseable output instead of tree view.

npm ls --parseable=true

You can list packages in the global install prefix instead of in the current project.

npm ls --global=true
npm ls -g // shorthand

You can find the full documentation here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
prosti
  • 42,291
  • 14
  • 186
  • 151
12

As the end of 2021, there are few obvious way to do it. Despite all the other answers are still working, I think an update is needed besides a more defined and complete list of commands possible, and while am I at it, I added some other common commands for whom needs it (install, uninstall, etc.)

# Bare command
npm list
# 'ls' is an alias of list
npm ls
# Don't show dependencies
npm list --depth=0
# Global modules
npm list -g --depth=0
# More info
npm la
npm ll
# Show particular environment packages
npm ls --only=dev
npm ls --only=prod
# Parseable view (tree view)
npm ls --parseable=true

The Node.js documentation is actually pretty well explained regarding the matter. This is a collective list of the main commands.

Before starting, note:


All commands will run the list of installed modules locally. In order to run at the global level, just add a -g flag at the end of the statement.


List installed dependency commands

  1. See the version of all installed npm packages, including their dependencies.

    npm list
    
     >>> /Users/joe/dev/node/cowsay
     └─┬ cowsay@1.3.1
       ├── get-stdin@5.0.1
       ├─┬ optimist@0.6.1
       │ ├── minimist@0.0.10
       │ └── wordwrap@0.0.3
       ├─┬ string-width@2.1.1
       │ ├── is-fullwidth-code-point@2.0.0
       │ └─┬ strip-ansi@4.0.0
       │   └── ansi-regex@3.0.0
       └── strip-eof@1.0.0
    
  2. Get only your top-level packages

     npm list --depth=0
    
  3. Get the version of a specific package by specifying its name.

     npm list <package-name>
    
  4. See what's the latest available version of the package on the npm repository

     npm view <package-name> version
    
  5. Install an old version of an npm package using the @ syntax

    npm install <package>@<version>
    npm install cowsay@1.2.0
    
  6. Listing all the previous versions of a package

    npm view cowsay versions
    [ '1.0.0',
      '1.0.1',
      '1.0.2',
      '1.0.3',
      '1.1.0',
      '1.1.1',
      '1.1.2',
      '1.1.3',
      ....
    ]
    

Update all the Node.js dependencies

  1. Install a new minor or patch release

     npm update
    
  2. Install a new minor or patch release, but do not update file package.json

     npm update --no-save
    
  3. To discover new releases of the packages, this gives you the list of a few outdated packages in one repository that wasn't updated for quite a while

     npm outdated
    

Some of those updates are major releases. Running npm update won't update the version of those. Major releases are never updated in this way, because they (by definition) introduce breaking changes, and npm wants to save you trouble.

To update all packages to a new major version, install the npm-check-updates package globally:

npm install -g npm-check-updates
ncu -u

This will upgrade all the version hints in the package.json file, to dependencies and devDependencies, so npm can install the new major version


Dev Dependency

Install in development dependencies.

npm install <package-name> -D
npm install <package-name> --save-dev # The same as above

Avoid installing those development dependencies in production with

npm install --production

Uninstalling npm packages

npm uninstall <package-name>
npm uninstall -g <package-name> # Globally uninstall
  1. Uninstall a package and remove the reference in the package.json file

      npm uninstall <package-name> -S
      npm uninstall <package-name> --save # The same as above
    

Some commands with global flag examples.

npm list -g
npm list --depth=0 -g
npm list <package-name> -g
npm view <package-name> version -g

Additional Commands

Documentation

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Federico Baù
  • 6,013
  • 5
  • 30
  • 38
11

One way might be to find the root directory of modules using:

npm root

Output:

    /Users/me/repos/my_project/node_modules

And then list that directory...

ls /Users/me/repos/my_project/node_modules

Output:

    grunt                   grunt-contrib-jshint

The user-installed packages in this case are grunt and grunt-contrib-jshint.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
obimod
  • 797
  • 10
  • 26
  • 2
    Why not use the official built-in npm command? `npm list -g --depth=0` – Andrew Camilleri 'Kukks' Jan 16 '15 at 12:41
  • This way might help the developer understand what's going on under the hood – obimod Jun 13 '16 at 17:35
  • 2
    This isn't 100% correct. When I run the command `npm root` it tells me: `Users/me/node_modules` which isn't correct. My node_modules lives somewhere else. I guess it depends on how you install node. I use `brew` to install my software. I'm guessing this is if you install node from their website using their package installer? – pixel 67 Dec 22 '16 at 10:35
  • I think it depends on your environment variables. Check out the $NPM... vars set in your enviro. I have a custom setup, using virtualenv and the activate hook to completely rewrite $PATH from scratch depending on what the project uses. – obimod Jan 19 '17 at 14:13
  • 2
    For me, my npm broke so I couldn't use the commands. I wanted to do a full reinstall so I needed to know which packages I had installed globally to rebuild. – bryjohns May 04 '17 at 20:01
  • I am using nvm for maintaining different node versions. This is the only command that worked for me. – kaushalpranav Dec 06 '20 at 11:01
11

Node.js has a concept of local modules and global modules.

Local modules are located within the current project directory.

Global modules are generally located at the user's home directory, though we can change the path where global modules reside.

  1. Lists local modules within current directory: npm list
  2. Lists global modules: npm list --global OR npm list --g // It will list all the top level modules with its dependencies
  3. List only the top level (installed modules) global modules: npm list -g --depth=0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Piyush Sagar
  • 2,931
  • 23
  • 26
10

>= v7.0.0:

npm ls -g

< v7.0.0:

npm ls -g --depth=0

Reference:

npm ls only prints the first level of dependencies by default. You can make it print more of the tree by using --depth=<n> to set a specific depth, or --all to print all of them.

For the latest release, see the npm documentation: npm-ls, depth

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
catwith
  • 875
  • 10
  • 13
9

I use npm -g outdated --depth=0 to list outdated versions in the global space.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pixel 67
  • 1,530
  • 18
  • 22
8

To see list of all packages that are installed.

$ npm ls --parseable | awk '{gsub(/\/.*\//,"",$1); print}'| sort -u

show parseable of npm packages list https://docs.npmjs.com/cli/ls#parseable

rab
  • 4,134
  • 1
  • 29
  • 42
8

You can try NPM Desktop manager:

NPM Desktop manager

With just one click, you can install/uninstall packages in dev or global status.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kant
  • 107
  • 1
  • 3
6

Folder node_modules contains user-installed packages so change the directory to node_modules and list the items. Core Modules are defined in Node.js's source in the lib/ folder.

Example:

cd ~/node_modules
ls

Output:

     express  maxmind-native  node-whois  socket.io  ua-parser-js
     geoip    mongoskin       pdfkit      tail       zeromq
     maxmind  nodemailer      request     ua-parser  zmq
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
karthick
  • 5,998
  • 12
  • 52
  • 90
5

For local modules, usenpm list --depth 0.

For global modules, use npm list -g --depth 0.

Example local npm module

Example global npm module

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jeyam Thillai
  • 71
  • 2
  • 12
5

Use npm list -g to get all the globally installed npm packages.

To get the npm packages of the current project then you can use "npm list" only.

enter image description here

Sayan Dey
  • 173
  • 1
  • 8
  • But that is exactly what the OP explicitly said they already knew and did not want. The question was how NOT to do that! Be sure, when answering old questions to bring new information that was not already present in one of the existing answer or the question itself. – chrslg Nov 29 '22 at 00:23
  • Use npm list -g to get all the globally installed npm packages. To get the npm packages of the current project then you can use "npm list" only. – Sayan Dey Jan 24 '23 at 10:38
4

Use npm list and filter by contains using grep

Example:

npm list -g | grep name-of-package
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Darlan Dieterich
  • 2,369
  • 1
  • 27
  • 37
4

As a shorthand, you can run:

npm ls -g --depth=0
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mwiza
  • 7,780
  • 3
  • 46
  • 42
0

I am using npm version 7.20.3, and it looks like the default depth is 0 already. So in my case, npm list --global showed only one installed package (npm). I knew I had a lot more packages installed, and I was puzzled at the output.

Eventually, I tried the --depth parameter and I was able to see all the packages installed: npm list --global --depth=1 to see the other packages installed (set to say 10 to see the whole dependency tree).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
HAltos
  • 701
  • 7
  • 6