Updating Dependencies Safely
You'll learn the crucial differences between npm update, npm install, and manually editing package.json — and when to use each.
The Three Ways to Update
When you need newer versions of your dependencies, you have three main approaches, each with different safety levels and behaviors.
npm update
Running npm update respects the version ranges already in your package.json. If you have "lodash": "^4.17.0" and version 4.17.21 is available, npm update will install it. However, it won't jump from ^4.x to ^5.x — it stays within your specified range. This is the safest automated approach because it honors semantic versioning boundaries you've already approved.
Think of it like your car's automatic software updates: it patches security issues and minor improvements, but won't suddenly upgrade you to a whole new model.
npm install <package>@<version>
Using npm install lodash@5.0.0 explicitly installs a specific version and updates your package.json. This gives you precise control and is ideal when you intentionally want a major version bump or a specific release. It's a deliberate, controlled upgrade.
Manually Editing package.json
You can change "lodash": "^4.17.0" to "lodash": "^5.0.0" directly in the file, then run npm install. This is similar to the previous method but separates the declaration from the installation. It's useful for batch updates or when working with version control, but requires that second step to actually fetch packages.
Safety Considerations
Always update your lockfile when upgrading. Use npm update for safe, incremental updates. Use explicit npm install commands when you're ready to handle breaking changes. Never just delete your lockfile and reinstall without understanding what versions will change.
Key Takeaway: npm update respects existing ranges safely, npm install <package>@<version> makes explicit changes, and manual edits require a follow-up install — choose based on how much control and risk you're comfortable with.