Published on

How to Start Coding with Node JS and NVM

Authors

Node Version Manager (NVM) is a CLI tool to manage versions of node js. You can manage mutiple vesions of node js on one environment. This is quite useful because you can switch to any version of node js with one command.

Install NVM

Installing NVM is quite easy as long as you have Homebrew on your Mac.

INFO: If you have no idea what Homebrew is, you can read this article first => You Need to Use Homebrew if You Use Mac

The following command below will install NVM:

brew install nvm

But there is one more step; if your current shell is zsh you need to edit ~/.zshrc and if your current shell is bash, you need to edit ~/.bashrc, adding the following scripts:

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

NOTE: you can check your current shell with the following command:

echo $SHELL

For your ease, you can just run the following command depending on your current shell.

For zsh

echo '\nexport NVM_DIR=~/.nvm\nsource $(brew --prefix nvm)/nvm.sh' >> ~/.zshrc && zsh

For bash

echo '\nexport NVM_DIR=~/.nvm\nsource $(brew --prefix nvm)/nvm.sh' >> ~/.bashrc && bash

After that, you should be able to run nvm command. You can check by running nvm --help command.

nvm --help

If the output is as follow, the installation is successful.

nvm --help

Node Version Manager (v0.38.0)

Note: <version> refers to any version-like string nvm understands. This includes:
  - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)
  - default (built-in) aliases: node, stable, unstable, iojs, system
  - custom aliases you define with `nvm alias foo`

 Any options that produce colorized output should respect the `--no-colors` option.

Usage:
  nvm --help                                  Show this message
    --no-colors                               Suppress colored output
  nvm --version                               Print out the installed version of nvm
  nvm install [<version>]                     Download and install a <version>. Uses .nvmrc if available and version is omitted.

# omitted below...

Install node

nvm ls-remote

First, let's check what version of node js is currently available with the following command:

nvm ls-remote

The output of the command above gives you all versions of node js currently available like below:

nvm ls-remote
        v0.1.14
        v0.1.15
        v0.1.16
        v0.1.17
        v0.1.18
        v0.1.19
        v0.1.20
        v0.1.21
        v0.1.22
        v0.1.23
        v0.1.24
        v0.1.25
        v0.1.26
        v0.1.27
# omitted...
       v14.17.4   (LTS: Fermium)
       v14.17.5   (LTS: Fermium)
->     v14.17.6   (Latest LTS: Fermium)
        v15.0.0
        v15.0.1
        v15.1.0
        v15.2.0
        v15.2.1
        v15.3.0
        v15.4.0
        v15.5.0
        v15.5.1
        v15.6.0
        v15.7.0
        v15.8.0
        v15.9.0

nvm install

And then, you can choose any vesion from all of the options above and install it with the following command:

nvm install <node version>

For demonstration purposes, we can install the latest LTS that is v14.17.6, the command will like below:

nvm install v14.17.6

nvm ls

After the installation, you can check the installed node js by running the next command:

nvm ls

In the output, you can see what versions of node js are alreadty installed and which version of node js is currently used:

nvm ls
       v14.17.6
->      v15.0.0

You see that v14.17.6 and v15.0.0 are already installed, and the one pointed by the arrow is the one currently used.

you can check if the selected version is really used with the following command:

node --version

v15.0.0

nvm use

If you want to switch to a defferent version of node js, you can use nvm use command like below:

nvm use v15.0.0
Now using node v15.0.0 (npm v7.0.2)

nvm uninstall

You can also install node js very easily by running the command below:

nvm uninstall <node version>

Conslusion

In this article, we learned how to install and use NVM to setup node js development environment on your computer. With NVM, you can install any version of node js and switch to any version very easily.

Now you can start coding with node js right away! Happy coding!