npm packages can be installed globally using the -g
or --global
flag so that the package can be used from the console.
To list all npm packages that you’ve installed globally on your system, you can use the npm list -g
command from your console:
npm list -g
# or
npm list --global
# for npm v6 and below
npm list -g --depth=0
Here’s an example of running the command on my console:
$ npm list -g
/Users/nsebhastian/.nvm/versions/node/v16.3.0/lib
├── @vue/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]
As you can see from the output above, the npm list
command will also print out the location where the packages are installed.
Sometimes, you may see other developers recommending you to use --depth=0
config when running npm list
command:
npm list -g --depth=0
The --depth=0
config is used to hide the dependencies of each installed package from the output. Since npm version 7, the npm list
command already hides the dependencies by default.
If you want to display the dependencies of your installed packages in version 7, then you need to use the --all
config:
npm list -g --all
Additionally, if you install an npm package in a custom location using the --prefix
config, then you need to add the same --prefix
config when running the the npm list
command.
For example, suppose you install the express package in a custom location as follows:
npm install express --prefix /Users/nsebhastian/Desktop/test/nodeWs
Add the same --prefix
command when calling npm list
command:
$ npm list --prefix /Users/nsebhastian/Desktop/test/nodeWs
[email protected] /Users/nsebhastian/Desktop/test/nodeWs
├── [email protected]
├── [email protected]
└── [email protected]
Conclusion
To display a list of installed packages in npm version 7, use the npm list -g
command.
For version 6 and below, use the npm list -g --depth=0
command.
By default, npm version 6 and below will print out the dependencies, which you can hide by using the --depth=0
command.
Starting from npm version 7, the dependencies of each installed package will be hidden from the output by default.
If you’re using npm v7, use the --all
config to display the dependencies of installed packages (or use --depth=1
to display only top-level dependencies. Your choice)
When you install packages in a custom location using the --prefix
config, then you need to add the same config when you call the npm list
command.