Chapter 2 ■ hello World
Chapter 2 ■ hello World
21
nvm
To start, let’s install nvm. This is the Node Version Manager that makes installation and switching between multiple versions of Node.js easy. This is not a mandatory step, but I’ve found that having nvm installed at the very beginning made my life easier when I had to start a new project and I wanted to use the latest and greatest version of Node.js at that point in time.
If you are using Mac OS or any Linux-based distribution, follow the instructions on nvm’s GitHub page at https://github.com/creationix/nvm. Windows users can follow nvm for Windows (please search for it in your favorite search engine), or install Node.js directly without nvm. Generally, I advise Windows users to install a Linux virtual machine (VM), preferably using Vagrant, and do all the server-side coding within the VM. This usually works best, especially when you want to deploy your code on a Linux server.
One tricky thing about nvm is knowing how it initializes your PATH. This works differently on different operating systems, so make sure you read up on the nuances.
Essentially, it adds a few lines to your shell’s initialization scripts so that next time you open a shell, your PATH is initialized and executes nvm’s initialization scripts. This lets nvm know about the different versions of Node.js that are installed, and the path to the default executable.
For this reason, it’s always a good idea to start a new shell right after you install nvm rather than continue in the shell where you installed it. Once you get the right path for your nvm, things follow smoothly.
Node.js
Now that we have installed nvm, let’s install Node.js using nvm. Visit the Node.js website to find out which version of Node.js fits the requirement: the Long Term Support (LTS) or the latest stable version. If you like to use the latest version, just type
$ nvm install node
An alternative is to choose the LTS version, which is assured to have support for a longer term than other versions. This means that although you cannot expect feature upgrades, you can expect security and performance fixes that are backward compatible, and you can continue to install incrementally newer versions without worrying about breaking your existing code. For this alternative, you have to type in the version number.
As of the writing of this book, 4.5.0 was the LTS version, so you can use it:
$ nvm install 4.5
All the code in this book was tested with Node.js version 4.5.0, so I recommend that you do the same. Also, don’t forget to make the just installed version of node the default.
For example,
$ nvm alias default 4.5
Chapter 2 ■ hello World
Otherwise, the next time you enter the shell, node will not be in your PATH. Or you may get the wrong version, if you have installed another version. Confirm the version of the node that’s been installed as your default by typing the following in a new shell / terminal:
$ node --version
Note that installing Node.js via nvm will also install npm, the package manager. If you are using Windows and installing Node.js directly, ensure that you have installed a compatible version of npm as well. Confirm this by noting down the version of npm that was installed along with Node.js:
$ npm --version
With Node.js 4.5, you are likely to see 2.15.9 as the npm version.
Project
Before we install any third-party packages with npm, it’s a good idea to initialize the project. With npm, even an application is considered a package. A package defines various attributes of the application. One such important attribute is a list of other packages that it depends upon. This will change over time, as we find a need to use more libraries.
But to start with, we need at least a placeholder where these things will be saved. For that, we need to initialize the package. Create a directory, say pro-mern-stack, navigate to it in your shell, and type the following:
$ npm init
Most questions that this command asks of you should be easy to answer. The defaults will work fine too. From now on, make sure you are in the project directory for all commands, especially npm commands (which I’ll describe below). This will ensure that all changes and installations are localized to the project directory.
npm
To install anything using npm, the command that you use is npm install <package>.
To start off, and because we need an HTTP server, let’s install Express using npm.
Installation of Express is as simple as
$ npm install express
Go ahead and install it. Once done, you will notice that it prints a tree of other packages Express depends upon. Now, uninstall and install it again with the --save options:
$ npm uninstall express
$ npm install express --save
Chapter 2 ■ hello World
23
■Note While installing, you can give a specific version to install using the suffix
@<version>, for example, npm install [email protected]. When not specified, it installs the latest version. If something is not working as described in this book, it may be due to a difference in version that you have installed vis-à-vis the version I used when writing the book. In such cases, you can try installing the same version as I have used for this book. For a list of the version of all packages, please refer to the final package.json file in the Github repository that contains the source code for this book.