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:
npm init --yes
Code language: Shell Session (shell)
Second, install the express
and mongoose
packages by running the following commands:
npm install express
npm install 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 and their dependencies of the current project as a tree-structure to the stdout:
npm list
Code language: Shell Session (shell)
Output:
The npm ls
is the shorter verison 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.
The following example lists all installed packages without their dependencies:
npm list --depth=0
Code language: Shell Session (shell)
Output:
Listing packages in dependencies
To display only dependency tree for packages in dependencies
, you use the --prod
or --production
flag like this:
npm list --prod
Code language: Shell Session (shell)
Output:
Note that the --prod
is the alias for --production
.
You can combine the --prod
and --depth
flags like this:
npm list --prod --depth=0
Code language: Shell Session (shell)
Output:
Listing packages in devDependencies
To show the dependency tree for packages in the devDependencies
, you use the npm list
command with the --dev
or --development
flag:
npm list --dev
Code language: Shell Session (shell)
Output:
The --dev
is the alias for the --development
.
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 --depth=0 --json
Code language: Shell Session (shell)
Output:
Summary
- Use the
npm list
to show the installed packages in the current project as a dependency tree. - Use
npm list --depth=n
to show the dependency tree with a specified depth. - Use
npm list --prod
to show packages in thedependencies
. - Use
npm list --dev
to show packages in thedevDependencies
. - Use
npm list --global
to list the global packages. - Use
npm list --json
to format the installed packages in the JSON format