How to check if an npm package is installed globally or locally?

In this tutorial, you will learn how to find if an npm package is installed globally or locally.

The methods covered in this tutorial work on Windows, Linux, and macOS.

Globally installed package

When you run the npm list command with -g flag, you get a list of globally installed packages.

$npm list -g
/usr/local/lib
|-- [email protected]
|-- [email protected]
| |-- [email protected]
| | |-- [email protected]
| | | |-- [email protected]
| | |-- [email protected]
| |-- [email protected]
| |-- [email protected]
| | |-- [email protected]
| | |-- [email protected] deduped
| | |-- [email protected] deduped
| | |-- [email protected] deduped
| | |-- [email protected]
| | |-- [email protected] deduped
| | |-- [email protected]
| | | |-- [email protected]
| | |-- [email protected] deduped
| | |-- [email protected] deduped
| | |-- [email protected]
| | | |-- [email protected] deduped
| | | |-- [email protected] deduped
| | | |-- [email protected] deduped
| | | |-- [email protected] deduped
| | |-- [email protected] deduped
| | |-- [email protected]

As you can see, NPM displays all globally installed packages in a tree view.

In case you don't want to see the dependencies of the globally installed packages, then use the --depth flag and pass 0 to it.

$npm list -g --depth=0
/usr/local/lib
|-- @angular/[email protected]
|-- [email protected]
|-- [email protected]
|-- [email protected]
|-- [email protected]
|-- [email protected]

Note: In NPM version 8.x, you don't have to add --depth=0 to the npm list command because, by default, the npm list -g command shows the globally installed packages without their dependencies.

When you want to check if a particular package is installed globally or not, then run the npm list command and pass the package name and -g flag to it. If the package is globally installed, it will show the version number of that package; otherwise, it will be empty. For example, suppose you want to check if nodemon package is installed globally or not, run the following command:

$npm list -g nodemon
/usr/local/lib
|-- [email protected]

As nodemon was installed globally, npm list command displayed the installed version number.

$npm list -g express
/usr/local/lib
|-- (empty)

express package is not installed globally; that is why the command showed empty.

Locally installed packages

To check if a particular package is installed locally or not, run the npm list command followed by the package name. If the package is installed, it will show its version; otherwise, empty will be displayed.

$npm list lodash
[email protected] /Users/mohitnatani/Desktop/nodejs-projects/notes-app
|-- [email protected]

To get a list of all the locally installed packages, then run the npm list command with no command-line options.

$npm list
[email protected] /Users/mohitnatani/Desktop/nodejs-projects/notes-app
|-- [email protected]
|-- [email protected]
| |-- [email protected]
| |-- [email protected]
| | |-- [email protected]
| |-- [email protected]
| | |-- [email protected]
| | |-- [email protected]
| | |-- [email protected]
| | | |-- [email protected]
| | | |-- [email protected] deduped
| | | |-- [email protected]
| | | |-- [email protected]
| | | |-- [email protected]
| | | |-- [email protected]
| | | | |-- [email protected] deduped
| | | |-- [email protected]
| | |-- [email protected]
| |-- [email protected]
| | |-- [email protected]
| |-- [email protected]
| |-- [email protected]
| | |-- [email protected]
| | |-- [email protected]
| |   |-- [email protected]
| |-- [email protected]
|-- [email protected]

If you don't want to see the dependencies of the packages, then add --depth=0 option to the npm list command.

$npm list --depth=0
[email protected] /Users/mohitnatani/Desktop/nodejs-projects/notes-app
|-- [email protected]
|-- [email protected]
|-- [email protected]

Recommended Posts