NVM
Nvm is a script that allows one to install more than one version of Node on a machine and switch between using those versions. There are two reasons to use it:
- You want to switch between Node versions
- You want to circumvent permissions problems when you install node modules globally.
Regarding the second point, typically, on an OSX machine, Node installs files globally at /usr/local/lib/node_modules and /usr/local/bin. When you try to do npm install [packagename] -g, you can run into errors like this:
npm ERR! Error: EACCES: permission denied, mkdir
Nvm will allow you to get around those permissions errors and install more than one node version and switch between those versions.
Background
It is easy to install Node packages globally. Hence, it can be easy to forget which Node packages are needed globally for a certain application. I think it’s possible that a Node application may work with some versions of a package, but not others. Therefore, I recommend developing a procedure for documenting the “Node environment”--that is, which version of Node is being used and which Node packages are installed globally--and find a way to ‘re-create’ a Node environment.
Nvm will help ‘re-create’ a Node environment.
What it does and some simple use cases
Nvm will allow us to install several different versions of Node on a machine and to choose the packages for that version of Node.
For example, if you wanted to be able to try Node v4.2.1 and Node v4.2.4 on your machine, you could install them as follows:
nvm install 4.2.1
nvm install 4.2.4
If you want to use yo v1.5.0 with Node v4.2.1, you could do this:
nvm use 4.2.1
npm install [email protected]
If you want to use yo v1.5.1 with Node v4.2.4, you could do this:
nvm use 4.2.4
npm install [email protected]
You can list the versions of node installed on your machine like this:
nvm ls
If you forget which version you are using, you can do this:
nvm current
Installation notes
To explain simply, installing nvm on your machine involves:
- Copying the code to your machine in a specific location
- Convincing your machine to find and use the code in the shell (command line)
The nvm github readme says you can accomplish both of the above with the following:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.30.1/install.sh | bash
This curl attempts to write to your shell script. Since I had previously “messed with” my shell scripts, I got a warning that .bashrc didn’t exist. So, I had to work on step #2 a bit; hence, the I use the word “convincing” in step #2.
For the record my .bashrc looks like this:
echo "running ~/.bashrc"
export NVM_DIR="/Users/razoyo-dev/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
and my .bash_profile looks like this:
echo "Executing .bash_profile"
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
The command echo $PATH is helpful for a bit of diagnosing. Mine looks like this after executing nvm use 4.2.1:
/Users/dev/.nvm/versions/node/v4.2.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Licensing
NVM is MIT licensing. According to the nvm read.me:
nvm is released under the MIT license. Copyright (C) 2010-2016 Tim Caswell
Advantages and Disadvantages
Advantages
- It allows one to have multiple node environments installed on a machine and to switch between them.
- It circumvents either having to use sudo or having to change permissions to get Node packages to install or execute.
- It provides a way to replicate a Node environment without ripping all of Node out of a machine.
- Using nvm is recommended by Digital Ocean, Near Form, and Competa.
- The developer is active. See below.
Developer
As of this writing, the last commit to the github repository was on 13 Nov 2016. I emailed him, and he emailed me back within the day. The physical address on his website is in Red Lick, TX, so I assume he is a native English speaking developer. He may even speak Texan.
| | Tim Caswell
306 Lewis Akin Road
Red Lick, TX 75503
214-250-6642
[email protected]
https://creationix.com/ |
| -- | -- |
Disadvantages
- Initially, there may be problems getting nvm to work because you have to get the shell set up.
- Nvm does not force you to have the right npm packages installed with the right version of Node. There isn’t anything to stop you from installing node modules that were developed for Node 5.0 with Node Version 1.3.15. (This isn’t really a weakness with nvm.)
- It will add a level of complexity to development, and something could go wrong with it.
Suggestion for improvement
It would be extremely handy if we could “name” environments. Right now we can switch between Node versions, but what if we have two different applications that use the same version of Node, but different versions of a node module? It would be handy if we could set up our development machine with the Node environment for Application A and call that environment “Application_A” and then switch to that environment by executing:
nvm use Application_A
Appendix A - Helpful CLI commands
This appendix is for me because I can’t remember this stuff.
To show the versions installed (either one will work):
nvm list
nvm ls
To see which version of node is currently being used (nvm list will also indicate the version of node being used by a green arrow: -> v4.2.1):
nvm version
To install a certain version (4.2.4, for example):
nvm intall 4.2.4
This will list all the globally installed packages:
npm list -g --depth=0
This will switch to use a node version. You must do this initially to use node:
nvm use 4.2.1
This will tell you your path (simple, but I keep forgetting how to do it):
echo $PATH
Appendix B - Tearing down a Node environment
In the process of researching nvm, I had to completely uninstall Node on Mac OSX. I used information from here: http://benznext.com/completely-uninstall-node-js-from-mac-os-x/
Note: There are some things on my machine that I use with Node, but I didn’t remove because I don’t think they are Node packages. These include:
- MongDB
- Sass
- Brew
- Compass
These are the steps I followed on my machine:
- go to /usr/local/lib and delete any node and node_modules
- rm -rf /usr/local/lib/node_modules
- go to /usr/local/include and delete any node and node_modules directory
- rm -rf /usr/local/include/node
- if you installed with brew install node, then run brew uninstall node in your terminal (I didn’t install node with brew.)
- rm -rf /usr/local/bin/node
- rm -rf /usr/local/bin/npm
Check your Home directory for any local or lib or include folders, and delete any node ornode_modules from there. ls -la
got me this:
drwxr-xr-x 3 razoyo-dev staff 102 Nov 30 12:19 .node-gyp -rw-r--r-- 1 razoyo-dev staff 26 Dec 3 13:53 .node_repl_history drwxr-xr-x 1261 razoyo-dev staff 42874 Dec 22 16:42 .npm
rm -rf ~/.node-gyp rm -rf ~/.npm rm .node_repl_history
go to /usr/local/bin and delete any node executable /usr/local/bin: bower jshint mongooplog sass brew lb-ng mongoperf sass-convert bsondump lb-ng-doc mongorestore sccjs compass learnyounode mongos scope-chains-closures eslint mongo mongosniff scss express mongod mongostat slc expressworks mongodump mongotop tree git-it mongoexport node-debug yo grunt mongofiles node-inspector heroku mongoimport nodemon
rm bower rm bsondump rm eslint rm express rm expressworks rm git-it rm grunt rm jshint rm lb-ng rm lb-ng-doc rm learnyounode rm node-debug rm node-inspector rm nodemon rm sccjs rm scope-chains-closures rm slc rm yo