DeveloperBreeze

In this tutorial, you'll learn how to update Node.js and npm to the latest version on an Ubuntu-based system. This is particularly useful when you're facing compatibility issues with outdated Node.js versions, or when your project requires newer features from the latest releases.

Step 1: Check Current Node.js and npm Versions

To begin, check the current versions of Node.js and npm installed on your system:

node -v
npm -v

If the version is outdated (e.g., Node.js v12 or earlier), it's time to upgrade.

Step 2: Remove the Old Version of Node.js (If Needed)

If you have an older version of Node.js, you can remove it to prevent conflicts during installation of the new version.

sudo apt remove nodejs

This command removes the old version of Node.js from your system.

Step 3: Add NodeSource Repository for Node.js

NodeSource provides an easy way to install and manage Node.js. To install a specific Node.js version, add the NodeSource repository for the version you want.

For Node.js 18 (LTS) (recommended):

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -

For Node.js 20 (latest version):

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -

This command adds the NodeSource repository to your system, allowing you to install newer versions of Node.js.

Step 4: Install Node.js

Once the repository is added, install Node.js:

sudo apt install -y nodejs

This installs Node.js and the latest version of npm that comes with it.

Step 5: Verify Node.js and npm Installation

After installation, verify the new versions of Node.js and npm:

node -v
npm -v

You should now see the updated versions of Node.js (v18.x or v20.x, depending on your choice) and npm.

Step 6: Clean npm Cache (Optional but Recommended)

Sometimes, older npm cache files can cause issues with new installs. Clean the npm cache to ensure a smooth experience:

npm cache clean --force

Step 7: Reinstall Project Dependencies (If Applicable)

If you're working on a Node.js project, you might want to reinstall your project's dependencies to ensure compatibility with the updated Node.js version. Navigate to your project directory and run the following commands:

  1. Remove node_modules and package-lock.json:
   rm -rf node_modules package-lock.json
  1. Reinstall dependencies:
   npm install

Conclusion

Updating Node.js and npm is an essential task for keeping your development environment up to date. Following this tutorial ensures that you’re running the latest, most secure, and feature-rich versions of Node.js and npm.

Feel free to restart any services or your system as needed after completing these steps.


Continue Reading

Discover more amazing content handpicked just for you

Tutorial

Build a Custom Rate Limiter in Node.js with Redis

{
  "error": "Too many requests. Try later."
}

Instead of IP address, use API keys for user-specific limits:

Apr 04, 2025
Read More
Tutorial
javascript

Using Node.js to Run JavaScript

  • Install a package:
     npm install lodash

Dec 10, 2024
Read More
Tutorial

Connecting a Node.js Application to an SQLite Database Using sqlite3

  • Embedded Systems: Devices with limited resources.
  • Small to Medium Applications: Applications that don't require the scalability of larger DBMS.
  • Development and Testing: Rapid prototyping without the overhead of managing a separate database server.

The sqlite3 package is a Node.js library that provides a straightforward API to interact with SQLite databases. It allows you to perform SQL operations such as creating tables, inserting data, querying data, and more, all from within your Node.js applications.

Oct 24, 2024
Read More
Tutorial
javascript

Creating a Component Library with Storybook and React

Storybook is an open-source tool that allows you to develop UI components in isolation and document them effectively. To add Storybook to your project, run the following command:

npx storybook init

Aug 27, 2024
Read More
Cheatsheet

Front-End Development Tools and Libraries Cheatsheet

No preview available for this content.

Aug 21, 2024
Read More
Tutorial
javascript

Integrating Vite with React in a Laravel Project: A Comprehensive Guide

   npm install @vitejs/plugin-vue @vitejs/plugin-pwa

Update your vite.config.js to include these plugins:

Aug 14, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Start the discussion!