Home » Tech Tips » Mac » How to Install and Uninstall NPM and Node.js in Mac Using Homebrew?

How to Install and Uninstall NPM and Node.js in Mac Using Homebrew?

Node.js is a powerful JavaScript runtime environment that allows you to run JavaScript on the server side, making it ideal for building web applications. NPM (Node Package Manager), on the other hand, is a package manager that simplifies the process of installing and managing Node.js modules and libraries. If you are dealing with Node.js related projects, then you need to set up and manage Node.js and NPM on your Mac. In this tutorial, we will explain the process of installing and uninstalling them on your Mac using Homebrew.

Installing Homebrew

Homebrew is a package manager for macOS that simplifies the installation and management of software and development tools. It allows you to easily install, update, and uninstall software packages, making it an invaluable tool for developers. Make sure you have administrator access and follow the below steps to install Homebrew on your Mac.

  • Open Terminal app on your Mac by going to “Applications > Utilities” folder or using Spotlight Search by pressing “Command + Space” keys.
Open Mac Terminal App
Open Mac Terminal App
  • To install Homebrew, paste the following command in the prompt and press enter. This command will initiate the Homebrew installation process:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  • During the installation, Homebrew will provide prompts and instructions and you will be prompted to type administrator password. Press “Enter” key when prompted to continue the installation.
Install Homebrew on Mac
Install Homebrew on Mac
  • After the installation is completed, you will be prompted to run two commands as next steps to add it to your shell’s PATH. These commands will make Homebrew readily accessible from your Terminal. Below are the syntaxes for those commands (make sure to change your user name) or copy them directly from your Terminal.
(echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> /Users/<user_name>/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"
Homebrew Installation Complete
Homebrew Installation Complete
  • Execute those commands one by one.
Homebrew Set to Path
Homebrew Set to Path
  • Now that, you have set the PATH of Homebrew, the setup has been completed. You can verify it by running the following command in the Terminal app and you should see the Homebrew version displayed.
brew –version
Check Homebrew Version
Check Homebrew Version
  • Once brew has been installed on your Mac, update it to ensure you have the latest package information. Type the below command in the prompt and press enter.
brew update
Update Homebrew
Update Homebrew

Installing NPM and Node.JS

Open Terminal app and simply run the following command. Once you execute this command, Homebrew will download and install the latest versions of Node.js and NPM. This might take few moments to finish.

brew install node
Install Node and NPM
Install Node and NPM

Note that if you wish to install a particular Node.js version, such as version 14, you can achieve this by executing the following command:

brew install node@14

Now, to verify that Node.js and NPM were installed successfully and to check their versions, use these commands:

  • node -v to check the Node.js version.
  • npm -v to check the NPM version.
Verify Node Version
Verify Node Version

Uninstalling NPM and Node.js

If you ever need to uninstall Node.js and NPM, you can do so using Homebrew’s uninstall command. Simply type the below command in the Terminal and press enter.

brew uninstall node
Uninstall Node
Uninstall Node

To ensure Node.js and NPM are completely uninstalled from your system, you can also run these optional commands to remove any leftover configuration files:

rm -rf /usr/local/lib/node_modules
rm -rf /usr/local/include/node
rm -rf /usr/local/include/node_modules
Remove Node Modules and Files
Remove Node Modules and Files

To confirm that Node.js and NPM have been successfully uninstalled, run the same version checking commands again. node -v should return “module”No such file or directory” error.

Node Version Error
Node Version Error

And, npm -v will show module not found errors.

Conclusion

That’s it!!! Now, you have learned how to install NPM and Node.js on Mac using Homebrew. You can also follow the steps outlined to uninstall them if necessary. These tools are essential for JavaScript development, and mastering their installation and removal is a crucial skill for any developer. Whether you are starting your coding journey or need to manage the development environment efficiently, you now have the knowledge and confidence to work with NPM and Node.js on your Mac.

Leave a Comment

Your email address will not be published. Required fields are marked *