Clean your Node.JS environment on OSX & handle multiple versions

Node.JS NPM

Overview

My dev environment consists of multiple platforms, consisting primarily of Node.JS applications as well as Appcelerator Mobile. Appcelerator uses Eclipse for developing cross-platform mobile applications and runs on top of Node.JS.

Unfortunately, Appcelerator (depending on the mobile SDK you’re using), requires an old version of Node.JS to properly run. This was not good for my standard Node.JS applications. At first I took a chance to share 1 version of Node for all my platforms, but sadly this could never be.

Thanks to a number of Google searches, I put together a pretty good recipe for cleaning up my environment and managing multiple versions of Node. Below is the process I followed that finally worked and had my environment stable across all my platforms (This was after about 15 install attempts that failed miserably).

Completely uninstall Node.JS

Node Version Manager is the awesome tool to install and manage multiple versions of Node.JS. Unfortunately, the Node version that was currently installed on my machine was not through NVM, so my first step was to completely uninstall Node.JS and start off fresh.

Thanks to this article on Stack Overflow, the 2 terminal commands that I used to uninstall Node.JS are:

lsbom -f -l -s -pf /var/db/receipts/org.nodejs.pkg.bom | while read f; do  sudo rm /usr/local/${f}; done
sudo rm -rf /usr/local/lib/node /usr/local/lib/node_modules /var/db/receipts/org.nodejs.*

Install Node Version Manager

Installing Node Version Manager is simple enough. Run the following command in Terminal:

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

Close and re-open Terminal to see if the NVM command can be found by entering nvm. If you’re presented with a series of NVM options, don’t celebrate yet. Restart OSX and repeat the above test to be sure. This is where nvm could not be found on my environment.

To fix this issue, I had to do the following:

  • Open Terminal and make sure you’re in the home directory by entering cd
  • Enter nano .bashrc. You should see an export script almost identical to the following:

export NVM_DIR=”/Users/johndoe/.nvm”
[ -s “$NVM_DIR/nvm.sh” ] && . “$NVM_DIR/nvm.sh”

  • Copy the export script and remove it from .bashrc
  • Save and Close the .bashrc file (CTRL+O – Enter – CTRL+X)
  • Next, enter nano .bash_profile to open the Bash Profile
  • Paste the export script you copied into the Bash Profile on a new line
  • Save and Close the Bash Profile (CTRL+O – Enter – CTRL+X)
  • Finally enter nano .bashrc to re-open the .bashrc file
  • Paste the following line into the file:

source ~/.nvm/nvm.sh

  • Save and Close (CTRL+O – Enter – CTRL+X)

Close and re-open Terminal to test if NVM exists. If yes, restart OSX and then do a final confirm if NVM can be found in Terminal. It was finally at this point that NVM was behaving properly on my machine and could be found in Terminal.

Install multiple versions of Node.JS using Node Version Manager

So, the worst is over. In my scenario, I wanted the latest stable release of Node as well as version 0.12.7. The latest stable release needed to be the default unless I temporarily change it to another version. To install these versions, do the following:

  • In Terminal, enter nvm install stable
    • This installs the latest stable version of Node.JS and also sets it to the default version
  • Next, enter nvm install 0.12.7 (or whatever version you want to install)

That’s it. You now have multiple versions of Node running. Whenever you want to add additional versions, be sure to use Node Version Manager.

Some useful NVM commands

Below are some NVM commands that you’ll use from time to time:

  • To switch between the versions of Node.JS you have installed – nvm use version (aka nvm use 5.0.0) – This temporarily sets the version you specified as the active version until you close Terminal
  • To make a version of Node.JS you installed as the default version – nvm alias default version (aka nvm alias default 5.0.0)
  • To display the list of node versions you have installed on your machine – nvm ls

Closing

That’s it folks. I hope this proves helpful. Good luck on your Node.JS quest.

Leave a comment

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