Starting Node.js Development

Page content

Notes on development with Node.js and JavaScript.

Installing Node.js

Node.js on Windows

To install Node.js on Windows, download it from the official Website. Choose the 64-bit Windows installer package for the current LTS version, unless you know that you need a different option.

Node.js on macOS

To install Node.js on macOS, use Homebrew.

Node.js on Linux

To install Node.js on Linux, use the recommended installation process for your distribution. Nodesource maintain the Linux packages for popular distributions.

If the Linux distribution supports snaps, you can also install Node.js as a snap. Snaps automatically update, so are not suitable for situations where you need a reproducible environment.

Node.js with Docker

Use the official Docker image for Node.js. The Node.js Foundation maintain these Docker images.

Linux and macOS: Global Package Installation

If you use macOS or Linux, follow the instructions in this guide to npm global without sudo to make global package installation use your home directory.

On these operating systems, the default configuration of Node.js causes the global option of npm to install packages to a shared location. This location requires administrator privileges to access, which means that every command will need unsafe levels of privileges.

Whenever possible use npx to run commands, rather than installing them globally.

Using Multiple Versions of Node.js

If you need to have multiple versions of Node.js on the same system, use nvm on Linux or macOS, and nvm-windows for Microsoft Windows.

Post-Installation Check

Once you have installed Node.js, run the npm doctor command in a terminal window:

npm doctor

This run various checks to verify that Node.js and Git are installed correctly, and tests whether the system can access the package registry for npm.

Tools Provided with Node.js

Every current Node.js installation includes the npm and npx command-line tools.

npm for Managing Projects

The npm tool helps you to manage your project throughout the development process.

To start a new project, use the npm init command. This creates the package.json file that describes your project.

You then use npm for your tasks, including:

Use the run-script feature to add appropriate custom npm commands to your project.

You also use npm to publish to package registries. If you are developing an application, remember to run the shrinkwrap command on the project before you publish it to a registry.

The npm tool is intended for use by automated systems as well as humans. Use the ci command to install clean versions of your projects for automated testing and deployment.

npx for Running Commands

The npx utility automatically finds and runs JavaScript command-line tools that you specify. If the tool is not already installed on your computer, npx automatically downloads and runs a temporary copy, without permanently installing it.

This means that you can type npx, followed by the commands for any tool that is available in the npm package registry, and npx should run that tool. For example, type this in a terminal to run learnyounode, an interactive tutorial program:

npx learnyounode

The npx utility only works with tools that have npm packages. However, you can use npx shx to run UNIX tools like ls on any operating system:

npx shx ls

This is not a special feature of the npx utility. It works because the ShellJS project publishes JavaScript implementations of the standard UNIX tools to the npm registry in a package called shx.

When you run npx in the working directory of a JavaScript project, it checks the commands that have been installed by the packages for that project. If the project has the command, npx runs that copy of the command.

If npx cannot find the specified command in the project, or you run npx outside of a project directory, then npx downloads a package for the command from the npm registry, and then runs the command. By default, npx downloads the latest version of the package. The packages that npx downloads are cached, so that each package is only downloaded once.

You can override the default behaviour of npx with command-line options. The optional -p flag for npx enables you to specify the name and specific version of the package that contains the command that you want to run. Use the –no-install flag to disable the feature to automatically download packages from the npm registry.

Essential Tools

There are a number of very popular utilities and libraries for JavaScript software development, but a few tools are so fundamental that you should install them even before you begin to write JavaScript code.

Git for Version Control

Ensure that Git is installed on your system. Git is now effectively the standard version control tool for developers, and npm also uses it for some operations.

Once Git is installed, IDEs and editor like Visual Studio Code will provide you with access to information and features from Git directly in their user interfaces. If you use Visual Studio Code, you should also consider installing the Git Lens extension, which enhances the integration with Git.

ESLint for Code Quality

Set up ESLint in all of your projects to run code quality checks. Add the ESLint plugin for Node to include specific checks for Node.js code.

Consider using ESLint with Prettier, a code formatting tool. Prettier integrates with ESLint, so that ESLint formats your code with Prettier first to remove style issues, and then checks the reformatted code for errors.

Plugins enable the popular text editors and IDEs to integrate ESLint and Prettier, so that your code can be formatted and checked as you work. If you use Visual Studio Code, install the ESLint extension and the Prettier extension. The Prettier extension includes a copy of Prettier, so it will work immediately.

Testing Tools

Jest provides a comprehensive set of tools for testing. If you do not already have a preference, add Jest to your project.

Existing JavaScript projects may use a combination of libraries in their test suite. The most popular solution is to use the Mocha unit testing framework, in conjunction with Sinon.js for mocks, Chai for assertions, Karma for running tests, and Istanbul to measure test coverage.

Writing Documentation

Use the Markdown format for separate documents, such as README files, and the JSDoc format for writing documentation in code. Many tools understand these formats.

The Documentation.js generator builds sets of documentation from JSDoc.

Learning Node.js

The NodeSchool project offers free interactive tutorials for Node.js and JavaScript. These include learnyounode, a tutorial for learning the basics of Node.js itself. Type this in a terminal to run learnyounode:

npx learnyounode

Other Resources