Installing Node.js

A guide to getting Node.js up and running on your machine.

Scroll down...

Content

Resources

Comments

This lesson takes you through the process of getting Node.js running on your machine. Luckily there are only a few steps to accomplish this task.

Installation

The creators of Node.js have made the process of installation super straight forward. You can easily get started with Node.js with their provided installers. This will install one version of Node.js on your system. However, you will most likely come to a point where you want to switch between Node.js versions. This is where NVM becomes useful.

Either method of installation will work, and if you install Node.js with the install wizard you always have the option of installing NVM later (and you most likely will). Next, we'll walk you through the installation and setup process.

The Node.js Install Wizard

Head over to the Node.js downloads page and choose the installer that applies to your operating system. Download it and follow the installation wizard to complete the install process.

Once completed open up your terminal and run $ node. You should see a new prompt indicating you are now in the Node.js REPL. REPL is really just a fancy word for shell. Now that you're in the Node.js execution environment, test it out with this snippet:

$ node
> console.log('Hello Node.js!');
#=> Hello Node.js!

If you are getting a node: command not found error you may have to add usr/local/bin to your $PATH. See this StackOverflow post.

You now have a default system Node.js version installed! But what if you want more than a single version of Node.js on your machine? This is quite common when switching between projects.

Node Version Manager (NVM)

Node Version Manager allows you to switch between Node.js versions on a project by project basis. There are a few different ways you can go about installing NVM.

Here we are going to favor the installation method for Mac OS X users. If you're on Windows or Linux have a look at the NVM README as the installation process is different. In the case of Windows, you'll be pointed to NVM Windows.

Homebrew

A simple method for installing NVM on a Mac is to use Homebrew. If you haven't installed it or used Homebrew yet, go ahead and follow the instructions to install on the link here. Installation of Homebrew should be as simple as copying and pasting their command into the terminal and running it.

Installing NVM with Homebrew

Now that you have Homebrew installed, use it to install NVM like this:

$ brew update
$ brew install nvm
$ source $(brew --prefix nvm)/nvm.sh

Explanation:

  1. brew update Updates Homebrew itself (recommended)
  2. brew install nvm Installs NVM
  3. source $(brew --prefix nvm)/nvm.sh Points to the correct nvm executable path

You should now get a list of options where you run $ nvm.

If you get a nvm: command not found error see this StackOverflow post.

This method works for your current shell session, but you want NVM to be loaded from the Homebrew-created path on every shell session. This is a simple fix.

Locate your ~/.bash_profile file. It should be in your home folder. If it doesn't exist create it and add the following lines:

# nvm
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh

Now open a new terminal window and you should be able to run $ nvm without running $ source $(brew --prefix nvm)/nvm.sh. Your .bash_profile file is loaded with every terminal session, so we put our initialization commands there to avoid calling them manually every session.

NOTE: If you're using a ~/.bashrc or another file to initialize your shell environment put the above commands there instead.

Using NVM

You now can switch between and install various versions of Node.js! Here are a few examples of how to use NVM:

# Get the currently enabled Node.js version
$ nvm current
system

# List available installed Node.js versions
$ nvm ls
         v5.8.0
         v5.9.1
->       system
node -> stable (-> v5.9.1) (default)
stable -> 5.9 (-> v5.9.1) (default)
iojs -> N/A (default)

# Run a file with a specific Node.js version
$ nvm run 5.8.0 index.js
Running node v5.8.0 (npm v3.7.3)

By now you should have NVM installed and you're ready to start exploring various versions of Node.js. Check out the resources for various useful links!

Code Review

Here are the important snippets of code from this lesson.

# Install NVM with Homebrew on Mac OS X

$ brew update
$ brew install nvm
$ source $(brew --prefix nvm)/nvm.sh


# Load NVM on every bash session

# ~/.bashrc
# nvm
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh


# Using NVM

# Get the currently enabled Node.js version
$ nvm current
system

# List available installed Node.js versions
$ nvm ls
         v5.8.0
         v5.9.1
->       system
node -> stable (-> v5.9.1) (default)
stable -> 5.9 (-> v5.9.1) (default)
iojs -> N/A (default)

# Run a file with a specific Node.js version
$ nvm run 5.8.0 index.js
Running node v5.8.0 (npm v3.7.3)

Wrapping Up

You now have Node.js installed and running on your computer as well as a tool to manage and switch between various versions of Node. You're most likely anxious to get started coding so let's take a look at some of the things Node can do next!



Sign up to track your progress for free

There are ( ) additional resources for this lesson. Check them out!

Sorry, comments aren't active just yet!

Next Lesson: Basic Node Functionality