How to list/debug npm packages?
Β© https://nodejs.org/en/

How to list/debug npm packages?

Different versions can cause version conflicts.

ByMario Kandut

honey pot logo

Europe’s developer-focused job platform

Let companies apply to you

Developer-focused, salary and tech stack upfront.

Just one profile, no job applications!

This article is based on Node v18.13.0

Installing and uninstalling dependencies are a core part of working with any Node.js project or framework React, Angular, NextJS, Express, Vue, etc. But how do you list your installed dependencies? If you are just starting with Node.js, have a look at the NodeJS intro article install node packages.

πŸ’° The Pragmatic Programmer: journey to mastery. πŸ’° One of the best books in software development, sold over 200,000 times.

If you work on different projects with different environments and different node versions, surely, some effort is needed to manage this, and version conflicts are anything but nice. For example, globally installed dependencies, can cause version issues in your project, if you use a wrong version. Or you are currently in the process of a major update in a project, and try to upgrade the dependencies, and all of a sudden you get an error when installing. Most likely, npm ERR! Invalid version in the log after running npm install. Then you have to start to debug and the fun starts.

How to fix version conflicts in installed NPM Packages?

The first step when facing version issues with installed packages is to delete and reinstall.

  1. Clean the node_modules folder rm -r node_modules
  2. Re-install packages with npm install

If the issue still persists clean NPM cache. You can forcefully clean the NPM cache with npm cache clean --force

In a lot of cases, the issue is resolved now. If it isn't, you have to start debugging. First you have to list your installed packages

How to list installed NPM Packages?

The first step is the NPM, you are going to look for the npm ls command.

The npm ls [[<@scope>/]<pkg> ...] will print to stdout all the versions of packages that are installed, as well as their dependencies when --all is specified, in a tree structure.

Let's create a small project and see what it prints out to stdout. Create a project with npm init and install a single dependency express.

mkdir npm-ls-test
cd npm-ls-test
npm init -y
npm i express

If you then run npm ls in the sample project folder, you will get the entire packages installed, when you install express.

[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]
  β”œβ”€β”¬ [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] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ └── [email protected] deduped
  β”œβ”€β”€ [email protected]
  β”œβ”€β”¬ [email protected]
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected]
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [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]
  β”‚ └─┬ [email protected]
  β”‚   β”œβ”€β”¬ [email protected]
  β”‚   β”‚ β”œβ”€β”€ [email protected]
  β”‚   β”‚ └── [email protected] deduped
  β”‚   β”œβ”€β”¬ [email protected]
  β”‚   β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚   β”‚ β”œβ”€β”¬ [email protected]
  β”‚   β”‚ β”‚ └── [email protected] deduped
  β”‚   β”‚ β”œβ”€β”€ [email protected]
  β”‚   β”‚ └── [email protected]
  β”‚   └── [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”¬ [email protected]
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected]
  β”‚ β”œβ”€β”€ [email protected]
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ └── [email protected] deduped
  β”œβ”€β”¬ [email protected]
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ β”œβ”€β”€ [email protected] deduped
  β”‚ └── [email protected] deduped
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”¬ [email protected]
  β”‚ β”œβ”€β”€ [email protected]
  β”‚ └── [email protected] deduped
  β”œβ”€β”€ [email protected]
  └── [email protected]

Without an argument npm ls lists all the dependencies down the dependency tree. If you are just interested in for example serve-static, you have to add an argument in the format name@version-range. Hence, you would do npm ls [email protected]. The stdout will be only for this package.

[email protected]
└─┬ [email protected]
  └── [email protected]

If you want to specify the depth in the dependency tree, add an argument for depth. The default level is infinity, which makes the stdout quite long. The stdout for npm-ls-test npm ls --depth=1, is only one level deep.

[email protected]
└─┬ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  β”œβ”€β”€ [email protected]
  └── [email protected]

If you have found the culprit package, which causes the version conflict, and re-installing and cleaning the cache didn't resolve it, you can use the overrides section in the package.json to make changes to a specific package, see NPM.

For example, we want to use a specific version for super-nice-package, we can simply add the following code to the package.json.

{
  "overrides": {
    "super-nice-package": "12.15.1"
  }
}

Version conflicts can also arise when for example a maintainer of a package is lacking a bit behind another major package. Usually, after some time, the maintainer updates it, and the version conflicts get resolved, but until this happens, you can use the overrides method.

Be aware, to regularly update your project, and remove the overrides, if possible.

How to list installed globally packages?

You can list globally installed packages with npm ls --global or npm ls -g.

For example, if you use nvm, the output should look something like this. In this example I used npm ls -g --depth=0 to list my global installed packages.

/PATH/.nvm/versions/node/v18.13.0/lib
β”œβ”€β”€ @angular/[email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
β”œβ”€β”€ [email protected]
└── [email protected]

Please be aware, that if you have to change a globally installed package for whatever reason (update cycle, security fixes, license updates, etc.) within a team of multiple developers, you have to communicate this to every relevant person.

TL;DR

  • The NPM CLI has a built-in command to list dependencies npm ls.
  • You can use the overrides section in the package.json to override specific dependencies.
  • When facing version conflicts, remove and reinstall dependencies, and/or forcefully clean the npm cache to fix it.

Thanks for reading and if you have any questions, use the comment function or send me a message @mariokandut.

If you want to know more about Node, have a look at these Node Tutorials.

References (and Big thanks):

NodeJS, NPM, Kentaro

More node articles:

Getting started with Webpack

How to list/debug npm packages?

How to specify a Node.js version

How to create a web server in Node.js

How to dynamically load ESM in CJS

How to convert a CJS module to an ESM

How to create a CJS module

How to stream to an HTTP response

How to handle binary data in Node.js?

How to use streams to ETL data?

How to connect streams with pipeline?

How to handle stream errors?

How to connect streams with pipe?

What Is a Node.js Stream?

Handling Errors in Node (asynchronous)

Handling Errors in Node.js (synchronous)

Introduction to errors in Node.js

Callback to promise-based functions

ETL: Load Data to Destination with Node.js

ETL: Transform Data with Node.js

ETL: Extract Data with Node.js

Event Emitters in Node.js

How to set up SSL locally with Node.js?

How to use async/await in Node.js

What is an API proxy?

How to make an API request in Node.js?

How does the Event Loop work in Node.js

How to wait for multiple Promises?

How to organize Node.js code

Understanding Promises in Node.js

How does the Node.js module system work?

Set up and test a .env file in Node

How to Use Environment Variables in Node

How to clean up node modules?

Restart a Node.js app automatically

How to update a Node dependency - NPM?

What are NPM scripts?

How to uninstall npm packages?

How to install npm packages?

How to create a package.json file?

What Is the Node.js ETL Pipeline?

What is data brokering in Node.js?

How to read and write JSON Files with Node.js?

What is package-lock.json?

How to install Node.js locally with nvm?

How to update Node.js?

How to check unused npm packages?

What is the Node.js fs module?

What is Semantic versioning?

The Basics of Package.json explained

How to patch an NPM dependency

What is NPM audit?

Beginner`s guide to NPM

Getting started with Node.js

Scroll to top ↑