Skip to content
Go back

How to Safely Update Dependencies with NPM, Yarn, PNPM, and Bun

The Ultimate Guide to Updating Project Dependencies with NPM, Yarn, PNPM, and Bun

That nagging feeling. You know the one. It’s the little voice in the back of every developer’s mind that whispers, “Your dependencies are probably outdated.” It’s easy to ignore, but keeping your project’s dependencies fresh is one of the most crucial maintenance tasks you can perform.

Why? Outdated packages can lead to security vulnerabilities, missed out on performance improvements, and a lack of access to new features and bug fixes.

But let’s be honest—updating dependencies can feel daunting. Fear of breaking changes can lead to procrastination. This guide is here to change that. We’ll walk you through the entire process, from basic checks to major overhauls, using the four most popular JavaScript package managers: NPM, Yarn, PNPM, and Bun.

Let’s turn that nagging feeling into a confident, streamlined workflow.

Before You Begin: The Essentials

Before you type a single command, let’s ensure you have a solid foundation.

1. Understand Your Lock File

Your project has two key files:

Rule #1: Always commit your lock file to version control.

2. Embrace Version Control (Git)

This is non-negotiable. Before you start any update process, create a new Git branch.

git checkout -b feature/update-dependencies

This gives you a safe sandbox. If something goes wrong, you can simply discard the branch and start over without affecting your main codebase.

3. A Quick Refresher on Semantic Versioning (SemVer)

Versions are written as MAJOR.MINOR.PATCH.


How to Update Dependencies: The Step-by-Step Guide

We’ll cover the same workflow for each package manager. The commands are slightly different, but the principles are the same.

Step 1: Check for Outdated Packages

First, see what’s out of date.

Package ManagerCommand
NPMnpm outdated
Yarnyarn outdated
PNPMpnpm outdated
Bunbun outdated

This command gives you a table showing the current version, the wanted version (based on your package.json), and the latest version available.

Step 2: Update Patch and Minor Versions (The “Safe” Updates)

This is the most common and safest type of update. It updates all packages to their latest minor and patch versions, respecting the version ranges in your package.json.

Package ManagerCommand
NPMnpm update
Yarnyarn upgrade (for Yarn 1) or yarn up (for Yarn 2+/Berry)
PNPMpnpm update
Bunbun update

After running this, your package-lock.json (or equivalent) will be updated with the new exact versions. It’s a good practice to commit this change:

git add .
git commit -m "chore: update patch and minor dependencies"

Step 3: Update to the Latest Versions (Including Major)

This is where the real power—and potential risk—lies. This process will update your package.json to the absolute latest versions of your dependencies, including major versions.

⚠️ Warning: This can and will likely introduce breaking changes. Proceed with caution and always test thoroughly.

Updating a Single Package

To update a single package to its latest version, regardless of SemVer rules:

Package ManagerCommand
NPMnpm install package-name@latest
Yarnyarn up package-name@latest
PNPMpnpm add package-name@latest
Bunbun add package-name@latest

Updating ALL Packages (The “Big Bang” Update)

Manually updating every package is tedious. This is where a fantastic tool called npm-check-updates (or ncu) comes in. It works across all package managers.

  1. Install ncu globally:

    npm install -g npm-check-updates
  2. Update your package.json: Run ncu in your project directory. It will show you what can be updated.

    ncu

    To actually write the new versions to your package.json, use the -u flag:

    ncu -u

    This command modifies your package.json but does not install anything yet.

  3. Install the new versions: Now, use your package manager’s install command to update the lock file and download the new packages.

Package ManagerCommand
NPMnpm install
Yarnyarn install
PNPMpnpm install
Bunbun install

Now you have a project with all dependencies updated to their latest versions. It’s time to test!

The Safe Workflow for Handling Major Updates

After a major update, your application might be broken. Don’t panic! Follow this systematic approach.

  1. Run Your Test Suite: This is your first line of defense.
    npm test
    # or yarn test, pnpm test, bun test
  2. Start Your App: See if it even runs.
    npm run dev
  3. Check the Console: Look for deprecation warnings or outright errors.
  4. Read the Changelogs: This is critical. If a major update broke something, go to the GitHub repository for that package and read its changelog or release notes for the version you just updated to. It will almost always tell you what changed and how to migrate your code.
  5. Fix and Repeat: Address the issues one by one. Often, a major update is just a renamed function or a changed prop.
  6. Commit the Change: Once everything is working, commit your work.
    git add .
    git commit -m "feat: upgrade react and react-dom to v19"

Advanced Techniques & Security

Security Audits

All major package managers have a built-in tool to scan for known security vulnerabilities.

Package ManagerCommand to CheckCommand to Fix
NPMnpm auditnpm audit fix
Yarnyarn audityarn audit (will prompt you to fix)
PNPMpnpm auditpnpm audit --fix
Bunbun audit(Fix by updating vulnerable packages)

Running npm audit fix (or its equivalent) will automatically update packages to versions that patch security vulnerabilities, often requiring a major version update.

Automate Updates with Dependabot

Manually checking for updates is repetitive. You can automate this! If you use GitHub, Dependabot is your best friend.

  1. Go to your repository on GitHub.
  2. Click on the “Insights” tab.
  3. Select “Dependency graph” and then “Dependabot”.
  4. Click “Enable Dependabot” and configure a dependabot.yml file.

Dependabot will automatically scan your repo for outdated dependencies and create Pull Requests for you to review and merge. This is the gold standard for maintaining a healthy project.

Quick Reference Command Cheat Sheet

ActionNPMYarn (Berry)PNPMBun
Check Outdatednpm outdatedyarn outdatedpnpm outdatedbun outdated
Safe Update (Patch/Minor)npm updateyarn uppnpm updatebun update
Update One to Latestnpm i pkg@latestyarn up pkg@latestpnpm i pkg@latestbun add pkg@latest
Install from package.jsonnpm installyarn installpnpm installbun install
Security Auditnpm audityarn auditpnpm auditbun audit
Run Testsnpm testyarn testpnpm testbun test

Best Practices: Your Dependency Checklist

Conclusion

Updating dependencies doesn’t have to be a source of anxiety. By understanding the tools, respecting the process, and leaning on automation, you can keep your projects secure, performant, and up-to-date with minimal fuss.

So, go ahead. Open up your terminal, create that new branch, and give your project the refresh it deserves. Your future self will thank you.


Share this post on:

Previous Post
Advent of Code 2025 (Day 11) - Navigating the Reactor: Mastering Graph Paths with DFS and Memoization
Next Post
Mastering the Bash Sleep Command: A Complete Guide with Examples