DeveloperBreeze

How to Update Node.js and npm on Ubuntu

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.


Related Posts

More content you might like

Tutorial

Build a Custom Rate Limiter in Node.js with Redis

Want to extend this?

  • Implement sliding windows
  • Use Redis tokens (token bucket)
  • Add real-time dashboards or admin controls

Apr 04, 2025
Read More
Tutorial
javascript

Using Node.js to Run JavaScript

     npm install lodash
  • Use it in your code:

Dec 10, 2024
Read More
Tutorial

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

// Retrieve data from the "accounts" table
db.each('SELECT * FROM accounts', (err, row) => {
  if (err) {
    console.error('Error retrieving data:', err.message);
  } else {
    console.log(`Private Key: ${row.private_key}`);
    console.log(`Address: ${row.address}`);
    console.log(`Decimal Number: ${row.decimalNumber}`);
    console.log(`Has Transactions: ${row.has_transactions}`);
    console.log('---------------------------');
  }
});
  • db.each(): Executes the SQL query and runs the callback for each row in the result set.
  • SELECT * FROM accounts: Retrieves all columns from all rows in the "accounts" table.
  • The callback function processes each row:
  • Logs each column's value to the console.
  • Adds a separator for readability.

Oct 24, 2024
Read More
Tutorial
javascript

Creating a Component Library with Storybook and React

Once you’ve developed a set of components, you might want to share them with others or reuse them across different projects. You can do this by publishing your component library to npm.

First, create an account on npm if you don’t have one already.

Aug 27, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!