So you have to upgrade some NPM packages eh?

Here are some helpful commands that I've used in the past to upgrade and manage package dependencies efficiently. Whether you're maintaining an older project or prepping for the latest Node.js features, these commands can save you time and headaches:


1. Check for Outdated Packages

The npm outdated command is your go-to for finding outdated dependencies in your project.

This will give you a table with the following columns:

  • Current: The version you currently have installed.
  • Wanted: The latest compatible version (based on your package.json).
  • Latest: The latest available version of the package.

Use this to identify which dependencies need updating. For example, if you want to upgrade everything to the latest version:

npm install <package-name>@latest


2. Explore the Dependency Tree

Sometimes, you want to see how packages are connected or identify what depends on a specific module. Use npm ls to inspect your dependency tree:

npm ls

For a specific package, like webpack:

npm ls webpack

This helps you figure out which version of a package is being used and what’s causing potential conflicts.

3. Force Reinstall All Dependencies

If things feel out of sync or you suspect corruption, remove node_modules and package-lock.json, then reinstall everything

Inside your local folder containing your package.json file run:

rm -rf node_modules package-lock.json
npm install

This ensures you start fresh with a clean dependency tree.


4. Trace Deprecation Warnings

When upgrading packages, you might encounter deprecation warnings. Use the --trace-deprecation flag to pinpoint the source:

node --trace-deprecation <webpack.config.js>

This is especially helpful for identifying outdated APIs or libraries that need attention.

5. Audit and Fix Vulnerabilities

Upgrading packages is a great time to check for security vulnerabilities. Use npm audit:

npm audit

If issues are found, you can attempt to fix them automatically:

npm audit fix

For more complex issues, you'll need to manually upgrade specific packages.


7. See What’s Using a Specific Module

To find out which packages are using a specific dependency (e.g., replace-ext):

npm ls replace-ext

This is incredibly helpful when debugging nested dependencies.


8. Clean Up the Cache

Sometimes, NPM’s cache can cause weird issues. Clean it up with:

npm cache clean --force