Logo

How can I update Node.js and npm to their latest versions?

The process of updating Node.js (the runtime) and npm (the package manager) varies based on your operating system and existing setup. Below are several methods to help you keep both Node.js and npm up to date.

1. Using Node Version Manager (nvm) — Recommended for macOS & Linux

If you’re on macOS or Linux, the most convenient way to install and manage multiple Node.js versions is through nvm (Node Version Manager).

a) Install or Update nvm

If you don’t already have nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash # Then restart your terminal or source your shell config

(Check the nvm repo for the latest install script.)

b) Install the Latest Node.js

nvm install node

This command installs the latest current version of Node.js.

To always use this version by default:

nvm alias default node

c) Update npm Separately (If Needed)

Once you’re on the new Node.js version, you can update npm to its latest version:

npm install -g npm@latest

(On some systems, you may need sudo privileges. However, with nvm, global installs don’t require sudo.)

2. Using nvm-windows (Windows)

On Windows, nvm-windows is an unofficial but popular port of nvm. It allows you to switch between Node.js versions easily.

a) Install nvm-windows

  1. Uninstall any existing Node.js versions if needed (optional but often recommended).
  2. Download the installer from the nvm-windows GitHub repository.
  3. Follow the on-screen prompts.

b) Install the Latest Node.js

nvm install latest nvm use latest

c) Update npm Separately

npm install -g npm@latest

3. Using Official Installer (macOS, Windows, or Linux)

If you prefer a one-time installation update (rather than version switching):

  1. Go to Node.js official downloads
  2. Choose the latest (Current) or LTS version’s installer for your OS.
  3. Run the installer; it will overwrite your previous Node.js installation.

After installation, Node.js and npm will both be updated to the version packaged with that installer.

Note: The official installer automatically includes a specific npm version. To get the absolute latest npm, you may still run:

npm install -g npm@latest

4. Updating npm Without Changing Node.js

If you only want to update npm (not Node.js), you can use npm itself to install the latest version globally:

npm install -g npm@latest

(If using system-installed Node.js on Linux/macOS, you might need sudo; if using nvm, you shouldn’t need sudo.)

Verify the update:

node -v # check Node.js version npm -v # check npm version

5. Best Practices & Tips

  1. Use LTS for Production

    • The LTS (Long-Term Support) version is recommended for production environments, as it receives critical bug fixes and security updates for an extended period.
  2. Multiple Node.js Versions

    • If you work on multiple projects requiring different Node.js versions, nvm (or nvm-windows) is crucial to quickly switch between versions.
  3. Check for Breaking Changes

    • Updating to a new major Node.js version might contain breaking changes. Review release notes if your app relies on certain native APIs or modules.
  4. Test Before Deploying

    • Always test your application in a staging environment after upgrading Node.js or npm to catch any potential incompatibilities.

Recommended Resources

Conclusion

To keep Node.js and npm at their latest versions:

  • Use nvm or nvm-windows to install and manage Node.js versions seamlessly.
  • Use the official installer if you only need a straightforward update to one version.
  • Upgrade npm itself by running npm install -g npm@latest.

Following these methods ensures you’re working with the newest features, performance improvements, and security patches.

CONTRIBUTOR
TechGrind