Getting started with NodeJS on Mac

Anand Sharma
4 min readNov 28, 2017

Basics of Node.js and getting it up and running on Mac OS the right way.

How to install and manage NodeJS

What is NodeJS

  • NodeJS is an open source platform for writing JavaScript outside the domain of browser.
  • It is a platform for running JavaScript on the server side, it is not a framework
  • It runs on Chrome’s v8 JavaScript engine and is available for most operating systems
  • The official nodejs site is at https://nodejs.org/en/
  • Node has one of largest eco-system of packages or modules for variety of needs. You can probably find a package or module for your needs
  • The official site for node packages/module is : https://www.npmjs.com/
  • nodejs is single threaded with event loop architecture, i.e, it has “event driven non blocking i/o model
  • Though it is single threaded, under the hood it uses libuv a multi-platform support library with a focus on asynchronous I/O
  • Under the hood libuv handles the threading, file system events, implements the event loop, features thread pooling etc.

If you are doing any front end or full stack development you need to be familiar with the basics of nodejs and how to manage different versions of it.

Nodejs ecosystem:

  • nodejs — official site
  • nvm — nodejs version manager, helps manage different versions of nodejs installed on your system
  • npm — nodejs package manager, helps install, upgrade, uninstall nodejs packages

Installing

Check to see if it is already installed?

| => node — version
v6.9.5
| => node -v
v6.9.5

Install directly from the site https://nodejs.org/en/

or

The recommended way (using homebrew) :

First check if brew is installed, if installed, you should get something like below.

| => brew -v
Homebrew 1.3.8
Homebrew/homebrew-core (git revision fc480f; last commit 2017–11–27)

If not installed then follow the instructions on homebrew site to install it.

After installation:

Good place to start with any command on *nix based system is <command> help:

| => brew help 
Example usage:
brew search [TEXT|/REGEX/]
brew (info|home|options) [FORMULA…]
brew install FORMULA…
brew update
brew upgrade [FORMULA…]
brew uninstall FORMULA…
brew list [FORMULA…]
Troubleshooting:
brew config
brew doctor
brew install -vd FORMULA
Developers:
brew create [URL [ — no-fetch]]
brew edit [FORMULA…]
https://docs.brew.sh/Formula-Cookbook.html
Further help:
man brew
brew help [COMMAND]
brew home

Commonly used commands from the above set (Note: use nvm to manage node, which is detailed later in this blog):

  1. brew install node — will installed the latest stable version of nodejs
  2. brew uninstall node — will uninstall the node package
  3. brew upgrade node — will upgrade the package to the latest stable version
  4. brew info node — will give info about the installed nodejs
  5. brew ls — will list the packages installed using brew
  6. brew config — will give the configuration details
  7. brew doctor — will give the status of the packages on your system
  8. brew install node — dry-run — the “ — dry-run” option lets you to see the what the result of the command will be, without actually running the command
  9. brew update — will check for the updates for the installed packages and brings everything up to date (Things change very fast in the node world and so using this command is a good way to ensure that you have the right updates installed
  10. brew home — will open up the homebrew site.

How to install and manage different versions of node?

Using brew to install nodejs is a good start, but if you are working with nodejs, then very soon, you will have a need to, manage different versions of node for different projects.

nodejs has a version manager nvm to mange and work with different versions.

Now that you have brew installed, use brew to install nvm:

| => brew install nvm

like before check that nvm (nvm -v does not work) is installed?

| => nvm — version
0.31.0

How to get nvm command help?

| => nvm help

How to install a particular version of node using nvm?

Command syntax “node install <version number>”

| => nvm install 9.2.0

How to list the different node versions being managed by nvm?

| => nvm ls
iojs-v3.3.1
-> v6.9.5
v9.2.0
system
default -> 6.9.5 (-> v6.9.5)
node -> stable (-> v9.2.0) (default)
stable -> 9.2 (-> v9.2.0) (default)
iojs -> iojs-v3.3 (-> iojs-v3.3.1) (default)

In the above the default (alias) is pointing to node version 6.9.5, alias “node” is pointing to version 9.2.0 and the current version being used is “-> v6.9.5

How to create a named alias for a particular version?

Example: Create an alias “latest” to point to installed node version v9.2.0

| => nvm alias latest v9.2.0latest -> v9.2.0

list the node version again to see the newly created alias “latest” pointing to node v9.2.0

| => nvm ls
iojs-v3.3.1
-> v6.9.5
v9.2.0
system
default -> 6.9.5 (-> v6.9.5)
latest -> v9.2.0
node -> stable (-> v9.2.0) (default)
stable -> 9.2 (-> v9.2.0) (default)
iojs -> iojs-v3.3 (-> iojs-v3.3.1) (default)

How to use a particular nodejs version?:

| => nvm use latest
Now using node v9.2.0 (npm v5.5.1)

--

--

Anand Sharma

We all started about 10,000 years back, on a human journey, destination unknown. A father, husband and an inquisitive soul.