Installing Node.js and npm on an Amazon EC2 instance

By Philip Knerr

Introduction

JavaScript isn’t just for browsers anymore. It has surpassed its humble beginnings as a tool to improve the user interfaces of websites. Today, powerful applications based on JavaScript may be executed on the server side via the Node.js platform. Moreover, heaps of useful functionality can be installed via the npm package manager. In fact, even applications which are not primarily JavaScript-based may depend on npm packages.

Therefore, you likely want to install the Node.js platform and the npm package manager on your EC2 instance.

This information is current as of November 16, 2017.

Requirements

This article assumes you have already instantiated an Amazon EC2 instance on which you would like to install the Node.js platform.

Selecting a version of the Node.js platform

Typically, two major versions of the Node.js platform are offered simultaneously. Even-numbered major versions, such as version 8.x, typically offer long-term support (LTS). Odd-numbered major versions, such as version 9.x, typically offer the latest features. It is up to you to select the best major version for your needs. This may depend on the following factors:

  • Whether long-term support is important in your use cases. (It may not be if you’re willing and able to upgrade the Node.js platform on a regular basis!)
  • Whether the newest features are important in your use cases. (You do like to live on the bleeding edge, right?)
  • Whether any Node.js applications you intend to execute depend on specific versions of the Node.js platform.

Within a given major version, you usually want the newest minor and patch versions. The following instructions will therefore retrieve the latest full version within the selected major version.

Installing the Node.js platform and the npm package manager

The good news is that the Node.js platform is available via its own repository. This repository is updated far more frequently than the standard Amazon Web Services repositories. As a result, you can cleanly install the newest Node.js version via the yum package manager.

The bad news is that you first have to enable this repository on your EC2 instance. To do so, execute the following command:

curl --silent --location https://rpm.nodesource.com/setup_#.x | sudo bash -

In this command, replace the # character with the major version number you selected. For example, you would replace the # character with:

  • 8 to install the 8.x LTS version.
  • 9 to install the 9.x version, which is the latest version as of this writing.

If an older version of the Node.js platform was previously installed, the yum package manager will get confused. It will look for the older version in the new repository and complain when it cannot be found there. To avoid this problem, clear the cached files for the yum package manager by executing the following command:

sudo yum clean all

Then, install the Node.js platform by executing the following command:

sudo yum install nodejs nodejs-devel nodejs-docs --enablerepo=nodesource

This command will also install the npm package manager.

In the above command, the following packages are optional, but recommended:

  • The nodejs-devel package installs tools which are useful for Node.js development. If you are only executing JavaScript code and not actually developing in JavaScript, you may be able to get away with omitting this package. If in doubt, you should include this package.
  • The nodejs-docs package installs the Node.js documentation.

To confirm that the Node.js platform was installed correctly, execute the following command:

node --version

The version number printed by this command should match the major version you selected. It should also match the full version number displayed on the Node.js home page.

You may also confirm that the npm package manager was installed correctly by executing the following command:

npm --version

Note that the npm version does not typically match the Node.js version.

Fixing the node-sass npm package

After upgrading the Node.js platform, the node-sass package is likely to get confused by the changed version. If this happens, it will display an error message similar to one of the following:

  • Error: Node Sass does not yet support your current environment.
  • Node Sass could not find a binding for your current environment: Linux 64-bit with Node.js 8.x

This issue is especially likely to affect Laravel applications, which use this npm package to compile Sass files into standard CSS.

Fortunately, this issue can easily be resolved by applying the clever fix documented by Joe Guaneri. The command should be executed for each application which depends on the node-sass npm package. It could also be executed globally if this package is installed globally.

Conclusion

Your EC2 instance now has the desired version of the Node.js platform and the npm package manager. Your EC2 instance can now execute the universal programming language of the Internet and leverage hundreds of thousands of npm packages.

–Phil Knerr