Sling Academy
Home/Node.js/NPM: How to Upgrade All Packages to the Newest Versions

NPM: How to Upgrade All Packages to the Newest Versions

Last updated: December 30, 2023

Explore some different ways to upgrade all packages to their latest versions in JavaScript/TypeScript projects (Node.js, React, Angular, etc).

Using npm-check-updates

This solution upgrades outdated packages by using a specific npm module. Here’re the steps:

  1. Install the npm-check-updates package globally: npm install -g npm-check-updates
  2. Run the tool to check for updates: ncu
  3. Upgrade your package.json to the latest versions: ncu -u
  4. Install the updated packages: npm install

TL;DR:

npm install -g npm-check-updates
ncu
ncu -u
npm install

Pros: Simple and straightforward to use. Cons: Requires an additional global package to be installed.

Using npm outdated and npm update

A native approach that utilizes npm’s built-in commands for checking and updating packages.

  1. Find outdated packages: npm outdated
  2. Update packages to their latest version: npm update

TL;DR:

npm outdated
npm update

Pros: No additional packages are needed. Cons: Might not update to the absolute latest versions if server prevents it.

Manually updating package.json

Directly editing the package.json to specify the desired versions and then installing the updates.

  1. Edit the package.json, replacing the version numbers with “*” or the latest version available.
  2. Run npm install to install the specified versions.

Example:

// In package.json before edit:
// "dependencies": {
//     "express": "^4.17.1",
//     ...
// }

// Edited package.json
// "dependencies": {
//     "express": "*",
//     ...
// }
npm install

Pros: Complete control over what versions are installed. Cons: More time-consuming and error-prone compared to other methods.

To Recap

In summary, there are multiple approaches to upgrading npm packages to their newest versions. Using npm-check-updates simplifies the process dramatically but requires additional installation. Native npm commands like npm outdated and npm update offer a straightforward solution without extra dependencies but may not update to the latest versions across major releases. Manually updating the package.json allows you to specify the exact versions but could be tedious and risky. Ultimately, the choice depends on the project requirements and the developer’s preference.

Next Article: Is it safe to delete the package-lock.json file?

Previous Article: Why not use Nodemon for production Node.js apps?

Series: Node.js Intermediate Tutorials

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates