npm install -g grunt-cli.
But before you can use Node.js or NPM you need to install them — while the NodeJS website includes an installer, there’s a better way to install them on a Mac. In this article, I’ll take you through the process of installing Node.js and NPM on a Mac using Homebrew. In another article, I’ll show you Windows users how to install them on the Windows operating system.
Before you install Node.js and NPM you’ll first need to have some familiarity with the Mac Terminal application. Terminal lets you dig into the underbelly of the operating system and issue text commands to your computer. You’ll need to use Terminal (or a similar application like iTerm) to not only install Node.js but also to use it and NPM.
Before you can install Node, you’ll need to install two other applications. Fortunately, once you’ve got these on your machine, installing Node takes just a few minutes.
- XCode. Apple’s XCode development software is used to build Mac and iOS apps, but it also includes the tools you need to compile software for use on your Mac. XCode is free and you can find it in the Apple App Store.
- Homebrew. Homebrew is a package manager for the Mac — it makes installing most open source sofware (like Node) as simple as writing
brew install node. You can learn more about Homebrew at the Homebrew website. To install Homebrew just open Terminal and type
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)". You’ll see messages in the Terminal explaining what you need to do to complete the installation process.
Observant readers will notice in the screenshot above that there’s an installer for NodeJS. You can download it directly from NodeJS.org. I recommend Homebrew over that installer for a few reasons:
- When installing Node via the installer, you have to use the
sudocommand to make sure it installs properly (there is a workaround for this, but it’s complicated).
sudolets the installer place files in areas of your file system that are only accessible to administrators. One nice thing about Homebrew is that it doesn’t require access to administrator-only areas of your computer in order to install NodeJS (or any other package). This is a safer approach as it makes sure that any package you install with Homebrew can’t wreak havoc on your computer.
- After installing Node via the installer you have to add the path to the node executable to your system $PATH. This involves mucking around with your shell login file. For experienced Terminal users or Unix-people this isn’t a big deal, but for those new to the command line that step can be daunting. Although the Homebrew method involves installing several different pieces of software, it’s generally just a simple process of point-click-and-wait. It takes a bit longer this way but there’s less room for error.
- Homebrew is a great tool for web developers. First, it makes removing Node very easy (otherwise you have to crawl through your file system and delete a bunch of files manually). Second, it greatly simplfies the installation of other useful packages like Git, Ruby, or the very useful wget utility.
Installing Node.js and NPM is pretty straightforward using Homebrew. Homebrew handles downloading, unpacking and installing Node and NPM on your system. The whole process (after you have XCode and Homebrew installed) should only take you a few minutes.
- Open the Terminal app and type
brew install node.
- Sit back and wait. Homebrew downloads some files and installs them. And that’s it.
To make sure you have Node and NPM installed, run two simple commands to see what version of each is installed:
- To see if Node is installed, type
node -vin Terminal. This should print the version number so you’ll see something like this
- To see if NPM is installed, type
npm -vin Terminal. This should print the version number so you’ll see something like this
How to Update Node and NPM
New versions of Node and NPM come out frequently. You can use Homebrew to update the software it installs.
- Make sure Homebrew has the latest version of the Node package. In Terminal type
- Upgrade Node:
brew upgrade node
How to Uninstall Node and NPM
You can use Homebrew to uninstall packages that it installed:
brew uninstall node
With Node.js and NPM installed you’ll soon be able to take advantage of the huge world of NPM modules that can help with a wide variety of tasks both on the web server and on your desktop (or laptop) machine. The NPM site lists all of the official Node packages making it easy to make the choice. Have fun and check out my current courses at Treehouse.