Summary: in this tutorial, you will learn how to use the npm list
command to list packages installed on your system.
Setting up a sample project
Let’s start by creating a sample project and installing some packages.
First, create a new directory called npm-demo
and run the npm init
command:
mkdir npm-demo
cd npm-demo
npm init --yes
Code language: Shell Session (shell)
Second, install the express
and mongoose
packages by running the following commands:
npm install express mongoose
Code language: Shell Session (shell)
Third, install the morgan
package as a development dependency by using the npm install
with the --save-dev
flag:
npm install morgan --save-dev
Code language: Shell Session (shell)
Introduction to npm list command
The npm list
command outputs installed packages of the current project as a tree structure to the stdout:
npm list
Code language: Shell Session (shell)
Output:
+-- express@4.21.1
+-- mongoose@8.8.0
`-- morgan@1.10.0
Code language: CSS (css)
The npm ls
is the shorter version of the npm list
command:
npm ls
Code language: Shell Session (shell)
If you use the npm la
or npm ll
command, the output will also include extended information.
Listing packages as a tree with a specified depth
To limit the depth of the dependency tree, you use the npm list
with the --depth
flag. For example, the following command lists all installed packages and their dependencies using depth 1:
npm list --depth=1
Code language: Shell Session (shell)
Output:
+-- express@4.21.1
| +-- accepts@1.3.8
| +-- array-flatten@1.1.1
| +-- body-parser@1.20.3
...
Code language: CSS (css)
Listing packages in dependencies
To display only the dependency tree for packages in dependencies
, you use the --omit=dev
flag like this:
npm list --omit=dev
Code language: Shell Session (shell)
Output:
+-- express@4.21.1
`-- mongoose@8.8.0
Code language: CSS (css)
You can combine the --omit=dev
and --depth
flags like this:
npm list --omit=dev --depth=1
Code language: Shell Session (shell)
Output:
+-- express@4.21.1
| +-- accepts@1.3.8
| +-- array-flatten@1.1.1
| +-- body-parser@1.20.3
...
`-- mongoose@8.8.0
+-- bson@6.9.0
+-- kareem@2.6.3
...
Code language: CSS (css)
Listing packages in the global packages
To list the global packages, you use the npm list
command with the --global
flag:
npm list --global
Code language: Shell Session (shell)
Formatting installed packages in JSON format
To format the output of the installed packages in JSON format, you use the npm list
command with the --json
flag:
npm list --json
Code language: Shell Session (shell)
Output:
{
"version": "1.0.0",
"name": "npm-demo",
"dependencies": {
"express": {
"version": "4.21.1",
"resolved": "https://registry.npmjs.org/express/-/express-4.21.1.tgz",
"overridden": false
},
"mongoose": {
"version": "8.8.0",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-8.8.0.tgz",
"overridden": false
},
"morgan": {
"version": "1.10.0",
"resolved": "https://registry.npmjs.org/morgan/-/morgan-1.10.0.tgz",
"overridden": false
}
}
}
Code language: JSON / JSON with Comments (json)
Summary
- Use
npm list
command to show the installed packages in the current project as a dependency tree. - Use
npm list --depth=n
command to show the dependency tree with a specified depth. - Use
npm list --omit=dev
command to show packages in thedependencies
. - Use
npm list --global
command to list the global packages. - Use
npm list --json
command to format the installed packages in the JSON format