Node Setup

Introduction

Installing node could be difficult & time-consuming before the emergence of Node Version Manager, or nvm. Nvm allows you to have multiple different versions of the node binary installed, facilitating easy switching between versions.

NVM

Go to https://github.com/nvm-sh/nvm to install nvm itself, then use it to install & manage any versions of node that you’d like to use. It handles the downloads, installs and version isolation.

Once it’s installed, you can use nvm like almost any package manager to install a new node version and list those already installed.

To install the latest node release, run this command:

nvm install node

To install a specific release level, for example Node 16, run this:

nvm install 16.3.0

If you install multiple versions, you will need to choose which version to use.

nvm use 16.3.0

This does some link magic that ensures all uses of ’node’ in commands resolve to that binary release version.

Node Modules

As you begin to develop in node, you will probably form strong opinions regarding node modules.

Node modules are sets of files that provide specific functionality packaged as libraries. Each module gets installed into a directory named

node_modules
at the level where the install was run. A module’s installation directory will contain all its dependencies, downloading and installing them at the same time as the parent module. This is powerful & clever in that every module will always have - within a relative PATH reference - all the supporting files it needs to provide the desired functionality.

Package.json

When you install a node module that your project will use (i.e. via a require(‘foo’)), you’re going to want to capture that requirement in the project’s package.json. This is a bill of materials for the project, listing project metadata like its name, author, whether it is public or private, and most importantly, the names & versions of the node modules installed.

If you use the ‘-S’ save flag when you install a node module, that module’s name & version number get written to the package.json file that is located in the same directory.

If you haven’t already created one, the first time you run npm install -S [foo], a package.json file will be created for you in that directory. It will only include a list of dependencies and for many projects, that should be fine. If you want or need the metadata included, you’ll want to initialize the project by running this:

npm init

Follow the prompts & answer the questions. When completed, you may want to edit package.json and add this line:

"private": true

That will keep you from accidentally publishing private work to the npm registry.

NPM

Now that you have a package.json file ready, it’s time to install a module. Modules are installed using Node Package Manager, or npm.

I mentioned npm above without explaining it. Npm is included in a node release while nvm is a third-party utility that provides functionality that node & npm don’t provide. During the course of a project, you’ll use npm much more frequently than nvm.

To install the popular Express web framework, run this command:

npm install -S express

As I said earlier, you will probably develop strong opinions about node modules.

The big downside is that there can be multiple node_modules directories in a project, with some - often lots of - duplication. This increases the size of a project & you don’t want or need all those node modules in your source code repository. It can be a bit of a management challenge.

You can avoid packaging and deploy time issues associated with using node modules by doing these three things consistently:

  • Ensure your package.json file is up to date & is stored in source countrol with the rest of your project
  • Don’t place your node_modules directories under source control
  • Always install your node modules after you pull the project from source control.

Installing required node modules right after cloning your project is possible if you used the ‘-S’ flag as discussed earlier. As long as you place your

package.json
into your repo alongside the rest of your code, it will contain a blueprint of all the modules & versions that are in use by your project.

Npm Install

If you’ve just cloned your project & it contains an up-to-date package.json file, just do this:

npm install

at the root of the project. Your node modules will be installed into the project and you’ll be ready to continue developing.

Using this technique, you keep all those node modules out of your repository. They’re already being maintained at npmjs.org, right?

Up Next

I’ll dig into another development topic that doesn’t usually receive much attention: planning.

0%