The Best Way To Reinstall Node.js (Mac/Linux/Windows)

One of the easiest ways how to install Node.js is to go to the official site, download the installer and install it. Later a developer can face a situation when he needs to switch to another version of Node.js or update it.

It's still possible to install it from the official site, but how many nodes already exist in a system? And how quickly switch between installed versions?

Maybe it's a good time to uninstall all of them and set up the system to be able to switch between nodes in seconds, always know the number of installed versions, and be able to delete any of them in one simple command.

How to uninstall Node.js from Mac OS

First things first, we need to uninstall old node versions and everything relative to it. You are lucky if you installed previous versions with Homebrew. The Homebrew method is one of the easiest ways to install or uninstall a node on Mac.

brew uninstall --force node

Type this command in the terminal. The brew will uninstall all installed versions of Node.js.

After that, it's better to run brew cleanup, it will remove all unused dependencies and folders.

brew cleanup

If your Node.js was installed differently, it's not a problem. You need to delete it manually. There is a bunch of folders, they can be removed one by one from the finder or a terminal.

List of folders, where Node.js and npm located or store their resources:

  • node and/or node_modules in /usr/local/lib

  • node and/or node_modules in /usr/local/include

  • node, node-debug, and node-gyp in /usr/local/bin

  • .npmrc in your home directory (it's npm settings, don't delete this if you plan on re-installing Node right away)

  • .npm in your home directory

  • .node-gyp in your home directory

  • .node_repl_history in your home directory

  • node* in /usr/local/share/man/man1/

  • npm* in /usr/local/share/man/man1/

  • node.d in /usr/local/lib/dtrace/

  • node in /opt/local/bin/

  • node in /opt/local/include/

  • node_modules in /opt/local/lib/

  • node in /usr/local/share/doc/

  • node.stp in /usr/local/share/systemtap/tapset/

In case you don't want to manually search for all these folders and files, you can run a single command in a terminal:

sudo rm -rf /usr/local/{lib/node{,/.npm,_modules},bin,share/man}/{npm*,node*,man1/node*}

This command doesn't touch your home directory, so you can later decide what to do with the rest files.

Now we can remove all global packages installed with npm:

rm -rf ~/.npm

After these commands, Node.js and npm will be fully removed from your system.

How to install Node.js on Mac

After clean-up, we can proceed with installing the new Node.js. But we will do it not in a direct way. Because if we install a certain version, we still will have an issue in the future with multiple nodes and maintaining.

To solve this issue, we should install an additional small tool: NVM.

For this tool, we have a single requirement — to have installed Command Line Tools. So if they are not installed in your system, you should run:

xcode-select --install

We are ready to install NVM. The easiest way to deal with it is a .sh script. Download and run this script we can with the next command:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash

0.37.2 is the latest version on the day of this writing. A version can be checked on NVM GitHub.

Of course, it can be installed manually. You need to clone a repository and add executable files to PATH. Detailed instruction described in NVM readme. This will be useful if you need to use NVM in the CI pipeline. I would recommend adding NVM to the Docker image which is used in your pipeline. More tips about improving CI performance I described in my previous article: Improving CI Performance (aka How To Save Your Money) How to improve CI/CD pipeline speed with using caching and Docker imagesmedium.com

Don't forget to restart the terminal window to refresh environment variables

We are almost done. Now we easily can install any version of Node.js. For example, this command will install the latest version:

nvm install node

If you wish to install the LTS version but with the latest npm, you can do next:

nvm install --lts --latest-npm

Instead of using flags like —-lts you can provide any version:

nvm install 8.9.1 # or 10.10.0, 12, etc

To see the list of installed versions you need to run the command:

nvm list

After installation, you need to select the default version for your system:

nvm use --lts

But what about windows users?

For windows available a very similar tool: Node Version Manager (nvm) for Windows.

It's a different project, but it realizes the same stuff. You still can install/uninstall/list and switch any version of Node.js.

Summary

With NVM you can achieve:

  • Easy way how to install/uninstall any version of Node.js

  • The best tool for switching between nodes

  • Removing is as easy as installing

You will feel the benefits in the future, especially when you will need to update a Node.js.

Thanks for reading!

Enjoyed this article?

Share it with your network to help others discover it

Continue Learning

Discover more articles on similar topics