CS 312 - Software Development

Installing Node


We will be making extensive use of node.js for this class. Some of you will already be familiar with it. Node is a JavaScript runtime engine that is not tied to a browser. It allows us to run JavaScript locally, and more critically later, develop a backend server so we can do full-stack JavaScript development.

While you can download and install node (and its associated package manager npm) directly from http://nodejs.org, I encourage you to not do so. node moves quickly, and it can be useful to be able to do version management.

I have been using Node Version Manager, which I encourage you to do as well. It will allow you to install different versions and quickly switch between them (if necessary). It is basically just a shell script that sets up your path and provides a tool for downloading and managing node/npm. You will find directions for downloading and setting it up at the link above.

Advanced Installation

There is no need to follow these directions. If you aren't comfortable on the command line, I would wait before jumping into this part.

The only downside of nvm is that when you open a new shell or log in, it takes a few extra seconds to run the setup function and setup your environment. That can be really annoying if you just want to do something quick that doesn't involve node.

So, I have reconfigured it a bit to only load when I need it. Rather than just running the script automatically, I put this in my .zshrc (and it should also work in your .bashrc)


# lazy loading nvm
setup_nvm(){
  unset -f node
  unset -f npm
  unset -f nvm
  export NVM_DIR="$HOME/.nvm"
  [ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
}

node(){
  setup_nvm
  node "$@"
}

npm(){
  setup_nvm
  npm "$@"
}

nvm(){
  setup_nvm
  nvm "$@"
}

Basically what this does is it defines functions for node, npm, and nvm. This is very fast, so the load time is not affected. If I call any of the three of those functions, it calls the setup_nvm() function. That function removes the definitions of the three stub functions, and then does the full load, putting the proper versions of node, npm, and nvm in my path. The first call to any of these will be slow, but I can accept that.