SkyShelf

Guides

How to Install Node.js and npm on Windows

How to Install Node.js and npm on Windows

Node.js lets you run JavaScript outside the browser, and npm, the package manager that comes with it, is how most JavaScript projects fetch their dependencies. Setting both up on Windows takes only a few minutes, but there are two decisions worth making deliberately: which release line to use, and whether to install directly or through a version manager. This guide follows the approach described in Microsoft's documentation for native Windows development and the project's own download options.

Decide where to run it

Before installing, choose between native Windows and the Windows Subsystem for Linux. Microsoft's guide suggests native Windows if you are new to Node.js, prefer PowerShell, or plan to deploy to Windows Server. WSL 2 makes more sense if you prefer Bash or deploy to Linux servers, because your development and production environments match. Whichever you pick, the guide advises keeping project files on the same file system where Node.js is installed, since crossing between the two can be slow. This article covers native Windows.

Pick a release line

Node.js publishes two kinds of releases that matter to you:

  • LTS (long-term support). The recommended choice for most people. It changes less often and is what tutorials, hosting platforms and tooling usually target.
  • Current. Newer features and more churn. Microsoft's guide describes it as useful for trying the newest improvements but more likely to cause issues than LTS.

If you are unsure, take LTS. The official download page labels the options clearly, and it is better to read the label there than to rely on a version number from any article, including this one.

Option A: the official installer

This is the simplest route for a single machine and a single project line.

  1. Open the official Node.js website (nodejs.org) and go to the download page.
  2. Choose the Windows installer for the LTS line, matching your processor type (most PCs are x64; some newer laptops are ARM64).
  3. Run the installer and accept the licence. The default location and options are fine for most people.
  4. If the installer offers to install extra build tools for native modules, you can decline unless you already know you need them; you can add them later.
  5. Finish the wizard, then open a new terminal window so that the updated PATH is picked up.

npm is included with Node.js, so there is nothing extra to install.

Option B: a version manager

Microsoft recommends using a version manager because Node.js versions change quickly and different projects often need different ones. The nvm tool popular on macOS and Linux does not support Windows, so the documentation points to nvm-windows, with Volta and nvs as alternatives. The documented steps for nvm-windows are:

  1. Remove any existing Node.js and npm installations first, including leftover folders such as the Program Files nodejs directory, because mixed installs cause confusing conflicts.
  2. Download the nvm-windows setup from its project releases page and run the installer.
  3. Open PowerShell and run the list command to see installed versions. There should be none yet.
  4. Look up the current LTS number with the available-versions command, then install it with the install command and that version number.
  5. Select it with the use command.

The guide also warns that nvm is intended for one developer's machine, not shared servers or build agents running several users, because it works through a symbolic link into a user's profile.

Verify the installation

Open a new terminal, ideally Windows Terminal, and run these two commands:

  1. node --version
  2. npm --version

Each should print a version number. If Windows says the command is not recognised, close every terminal and open a fresh one; if it still fails, the PATH entry was not added, and rerunning the installer usually fixes it.

Run your first script

Create a folder for a test project, then a file named hello.js containing a single line that logs a message. Run it with node hello.js and you should see the message printed. To start a project that uses packages, run npm init -y in the folder to create a package.json file, then npm install followed by a package name to add a dependency. The packages are stored in a node_modules folder, which you should not commit to version control.

Add an editor and Git

Microsoft's guide recommends Visual Studio Code as the editor, with its built-in terminal and debugger, and lists other editors as alternatives. It also recommends installing Git for Windows so that VS Code's Source Control panel works and so you can clone projects. The Git installer's default answers are fine for beginners. Add a .gitignore that excludes node_modules before your first commit.

Common problems

PowerShell refuses to run npm scripts. Windows may block script files under its default execution policy. Read Microsoft's documentation on execution policies and change it only for your own user account if you understand the implications, or use Command Prompt as a quick alternative.

Two different versions show up. An old installation is earlier in PATH. Uninstall the old one, or if you use a version manager, remove the standalone installation first as the guide advises.

Permission errors on global installs. Avoid running everything as administrator. Install packages per project where possible, and reserve global installs for command-line tools you use everywhere.

Slow installs inside synced folders. Keep projects out of cloud-synced directories, since node_modules contains very many small files.

Keeping up to date

With the installer, download the newer LTS installer when a new LTS arrives; it upgrades in place. With a version manager, install the new version and switch to it. In both cases, check a project's own documentation for the Node version it expects before upgrading.

Comments

No comments yet.

Code