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 --yesCode language: Shell Session (shell)Second, install the express and mongoose packages by running the following commands:
npm install express mongooseCode 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-devCode 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.0Code language: CSS (css)The npm ls is the shorter version of the npm list command:
npm lsCode 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=1Code 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=devCode language: Shell Session (shell)Output:
+-- express@4.21.1
`-- mongoose@8.8.0Code language: CSS (css)You can combine the --omit=dev and --depth flags like this:
npm list --omit=dev --depth=1Code 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 --globalCode 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 --jsonCode 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 listcommand to show the installed packages in the current project as a dependency tree. - Use
npm list --depth=ncommand to show the dependency tree with a specified depth. - Use
npm list --omit=devcommand to show packages in thedependencies. - Use
npm list --globalcommand to list the global packages. - Use
npm list --jsoncommand to format the installed packages in the JSON format