©  Caio Ribeiro Pereira 2016

Caio Ribeiro Pereira, Building APIs with Node.js , 10.1007/978-1-4842-2442-7_2

2. Setting Up the Environment

Caio Ribeiro Pereira

(1)São Vicente - SP, São Paulo, Brazil

In this chapter, we are going to install Node.js on the most used OSs (Windows, Linux, and MacOSX). However, the entire book uses MacOSX as the default OS. Don’t worry about the differences among these OSs, because all examples in this book are compatible with all the three OS platforms.

Node.js Standard Installation

Setting up the Node.js environment is much the same despite the OS. Just a few procedures will be different in each OS, especially for Linux, because in the Linux environment you can compile an application or install it using a package manager, but this is not a major change.

The first step is to access the official Node.js web site at http://nodejs.org . On the home page, shown in Figure 2-1, v6.9.1 LTS Stable to download the latest compatible version for Windows or MacOSX. If you use Linux, I recommend you read the quick wiki at https://github.com/nodejs/node/wiki/Installing-and-Building-Node.js , which explains how to compile and install Node.js in some popular Linux distros.

A435096_1_En_2_Fig1_HTML.jpg
Figure 2-1. Node.js home page

After the download, install the software. If you use Windows or MacOSX, just click through the installation wizard screens until the installation is completed; after all, there is no specific configuration required .

To test if everything is running correctly, just open the terminal (for Linux or MacOSX), command prompt, or Power Shell (for Windows) and type this command:

1   node -v && npm -v

This checks the version of Node.js and NPM. This book uses Node v6.9.1 LTS and NPM 3.10.8.

About io.js and Node.js Merge

As of September 2015, Node.js was upgraded from 0.12.x to version 4.0.0 because of the merge with io.js. The io.js was a fork built and maintained by the Node.js community. They worked to include some features from ECMAScript 6 implementation, in addition to making several improvements that were slow on Node.js. When io.js reached version 3.0.0 and both groups decided to merge io.js into Node.js, the result was the Node.js v4.0.0. This merger not only gave a huge upgrade to this platform, but also resulted in a more stable and reliable version of Node.js.

Note

This book uses Node v6.9.1 LTS, with a lot of ES6 codes. You can learn more about ECMAScript 6 at http://es6-features.org .

Node.js Installation Via NVM

Tip

In this section, we are just going to explore an alternative way to set up Node.js using a version manager. Feel free to skip this step if you don’t need to or don’t want to learn how to use the NVM.

Just like Ruby language has the Ruby Version Manager (RVM) to manage multiple Ruby versions, Node.js also has a manager, the Node Version Manager (NVM).

NVM is a perfect solution to test your projects in different Node.js versions. It is also useful for people who like to test unstable or the latest versions. NVM has great advantages: It is practical, it is easy to use, it can uninstall a Node.js version with a single command, and it will save you time searching and installing a versions of Node.js. It is a good alternative, especially on Linux, because its native package managers are frequently outdated, which invalidates a new Node.js version to be installed.

Set Up NVM

In few steps you can set up NVM for a MacOSX or Linux system; you just need to run this command:

1   curl https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash

Unfortunately, the official version of NVM is not available for Windows, but there are alternative projects created by the community. Here are two similar alternatives for Windows:

Both have a similar command-line interface (CLI) inspired by NVM.

Top NVM Commands

As as a sort of recipe, here is a list of the top commands that will be essential to manage multiple versions or at least keep your environment updated with the latest version.

  • nvm ls: Lists all versions installed.

  • nvm ls-remote: Lists all the Node.js versions available to download and install.

  • nvm install vX.X.X: Downloads and installs a version.

  • nvm uninstall vX.X.X: Uninstalls an existing version.

  • nvm use vX.X.X: Chooses an existing version to use.

  • nvm alias default vX.X.X: Chooses an existing version to be loaded by default in the OS boot time.

Note

Replace the vX.X.X with the Node.js version of your choice, such as v6.9.1.

Installing Node.js Via NVM

To install Node.js via NVM, you need to run the following commands:

1   nvm install v6.9.1
2   nvm use v6.9.1
3   nvm alias default v6.9.1

After running these commands, Node.js will be installed and ready to use.

Conclusion

Congratulations! Now, besides having Node.js installed and working, you also learned a new way to manage multiple Node.js versions via NVM. In the next chapter, we are going to explore some important things about Node Package Manager (NPM) , so keep reading, because the fun is just starting!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset