Skip to content

Instantly share code, notes, and snippets.

@raphodn
Last active May 7, 2018 07:31
Show Gist options
  • Save raphodn/0a1603378b4f8b4c562da64ce1e7ca24 to your computer and use it in GitHub Desktop.
Save raphodn/0a1603378b4f8b4c562da64ce1e7ca24 to your computer and use it in GitHub Desktop.
NVM: Installation, Usage, Tips

NVM: Node Version Manager

Installation

https://github.com/creationix/nvm#installation

Ubuntu

Usage

Basic

Get NVM (package) version

nvm --version

List all locally installed versions

nvm ls

List all available versions to install

nvm ls-remote

Install specific version

nvm install <NODE_VERSION>
// nvm install 8.11.0

Check current node installed

nvm current
node -v

Check where the current node version points to

which node
which npm
npm root -g
  • In Ubuntu it returns:
/home/<USERNAME>/.nvm/versions/node/v8.11.0/bin/node
/home/<USERNAME>/.nvm/versions/node/v8.11.0/bin/npm
/home/<USERNAME>/.nvm/versions/node/v8.11.1/lib/node_modules

If you have multiple installed versions of node, you can switch between them using

nvm use <NODE_VERSION>

Update node

Update from <OLD_NODE_VERSION> to <NEW_NODE_VERSION> (and reinstall all your global packages !)

nvm install <NEW_NODE_VERSION> --reinstall-packages-from=<OLD_NODE_VERSION>
// nvm install 8.11.1 --reinstall-packages-from=8.11.0

You can now uninstall the old version (e.g. 8.11.0)

nvm uninstall 8.11.0

Watch out to update the default alias if needed

nvm alias default 8.11.1

Make global node_modules easily accessible

You want to used a globally-installed (-g) package in your Node REPL, but the require() commands returns Error: Cannot find module ?

For the current shell

export NODE_PATH=$NODE_PATH:`npm root -g`

For all shell when opened (source)

// copy the line to your ~/.bashrc file (below the 3 lines concerning NVM_DIR)
export NODE_PATH=$NODE_PATH:`npm root -g`

Also note that NVM not updating the $NODE_PATH is intentional

Check that $NODE_PATH is correct

echo $NODE_PATH
  • in Ubuntu it returns:
:/home/<USERNAME>/.nvm/versions/node/v8.11.1/lib/node_modules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment