NPM
JavaScript
Node.js
Programming
Web Development

How to list npm user-installed packages?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Node.js is an essential tool for modern web development which incorporates the use of npm (Node Package Manager) to manage dependencies and packages efficiently. Understanding how to manage these packages, especially determining which packages have been installed by the user, versus those pulled in by other packages, is crucial for maintaining a clean and effective development environment.

Introduction to npm

npm not only manages dependencies but also provides utilities to help developers keep track of the packages installed in a project. These packages can be either:

  • Local: Installed in the node_modules directory of a project.
  • Global: Installed globally on your computer, accessible from any project.

Listing Installed npm Packages

Local Packages

To list the npm packages installed locally in your project, simply navigate to your project's directory in the terminal and run:

bash
npm list

This command will show the entire tree of packages installed for the current project. However, this includes all dependencies, including those installed by other packages. To filter and show only the packages that you explicitly installed, you can use:

bash
npm list --depth=0

This command limits the dependency tree to the top level, showing only the packages listed in your package.json under dependencies and devDependencies.

Global Packages

For global packages, use the -g flag with the npm list command:

bash
npm list -g

Similarly, to see only the top-level global packages you installed, combine this with the --depth=0 flag:

bash
npm list -g --depth=0

Detailed Examples

To demonstrate, if you install packages like express and nodemon locally, and webpack globally, running these commands might show outputs like:

  • Local:
    $ cd your_project_directory
    $ npm list --depth=0
    Output might include:
 
  your_project_name@1.0.0 /path_to_your_project
  ├── express@4.17.1
  ├── nodemon@2.0.7
  • Global:
    $ npm list -g --depth=0
    Output might include:
 
  /usr/local/lib
  ├── npm@6.14.8
  ├── webpack@4.44.2

Understanding npm Version Numbers

Packages listed come with version numbers, adhering to the semantic versioning system (<major>.<minor>.<patch>). This makes it easy to track updates and compatibility:

  • Major: Major changes, possibly breaking backward compatibility.
  • Minor: Minor changes, adding functionality in a backwards-compatible manner.
  • Patch: Bug fixes and minor changes which do not affect compatibility.

Useful Tips

  • Checking outdated packages: Run npm outdated. This checks your current packages against the npm registry and lists any versions of packages that have newer versions available.
  • Cleaning up unused packages: If you delete a package manually from your package.json or just want to clean up, run npm prune to remove packages that are no longer referenced in package.json.

Summary Table of Commands

CommandDescription
npm listDisplay the local dependency tree
npm list --depth=0Display only the top-level local dependencies
npm list -gDisplay the global dependency tree
npm list -g --depth=0Display only the top-level global dependencies
npm outdatedCheck for newer versions of the packages
npm pruneRemove unused packages

Utilizing these commands effectively requires regular practice and integration into your workflow. This not only maintains the health of your project’s dependencies but also ensures you have a grip on the tools that your application requires to function effectively and efficiently.


Course illustration
Course illustration

All Rights Reserved.