DEV Community

Otavio Monteagudo
Otavio Monteagudo

Posted on • Updated on

Set Up Windows for Node.js Development with NVM

Intro

So you want to develop using the Javascript run-everywhere platform in the same computer in which you game, edit videos, code C# desktop apps or whatever. You are also aware that there are multiple node.js versions in active development and it is fairly common to find projects in the wild that only run in a handful of them. Then this guide is for you, let's set up a Windows machine for node.js version with multiple version management, while also addressing common pitfalls.

Install Windows Terminal

If you are using Windows 11, good news: you already have the Windows Terminal installed. If not, open the Microsoft Store and download it free of charge.
Microsoft Store
This is a hardware-accelerated tabbed terminal from which you can run Powershell, CMD or WSL interfaces, and a touchstone (and, some would say, pretty belated) step to approach Windows development experience to other major OS's. Installing this terminal is highly recommended if you plan to develop on Windows, using node.js or else.

Install NVM for Windows

Now, instead of installing node.js from the official website, we should install the Node Version Manager and download node versions from there. In case you already have node installed this should not be a major problem as NVM will overwrite any node-related environment variables and symlinks, but I'd recommend you to uninstall it anyway as this process will render the current installation completely useless.
Go to the NVM for windows project page and download the latest available version's nvm-setup.zip from the releases page.
Note that this is not the same as the UNIX-based NVM project, although it is functionally equivalent. "Similar, not identical" as the project itself discloses.
Unzip the folder's contents and run nvm-setup.exe. You'll be prompted to agree with the project's terms of use (currently it's the MIT License), then the installer will ask where to install nvm, which will also be the same location as the downloaded node versions and their globally-enabled packages; the roaming app data folder under your current user should be perfectly fine.
However, you'll then be prompted to indicate where to keep the node.js symlink, and (at least in versions up to 1.1.8) there's a catch: you cannot keep the symlink under a path which contains whitespaces, and unfortunately the default installation path (currently C:\Program Files\nodejs) steps right into this trap.

NVM symlink path

This is where I've installed my local NVM. It's only a suggestion and you can install wherever you like (as long as the path does not contain whitespace), I'd only recommend the target folder's name to be something like \nodejs so you don't end up losing the installation, which can be removed directly from the standard uninstaller anyway.

Install Node & Setup NVM

First of all, you need to run Windows Terminal with administrative privileges. One way this can be done is by looking for the terminal in the system's internal search, clicking with the right button on its icon and then selecting 'Run as Administrator'.

Running Windows Terminal as an Administrator

Any time you feel lost while fiddling with NVM, simply type nvm in the terminal and a very concise manual will pop up explaining each available command and their parameters.

NVM Manual

Let's make sure NVM is enabled, run:

nvm on
Enter fullscreen mode Exit fullscreen mode

And after that, let's install the current Long Term Support version, pre-aliased as lts (currently 14.18.1):

nvm install lts
Enter fullscreen mode Exit fullscreen mode

After the installation finish, we must declare to NVM what version we'd like to use:

nvm use 14.18.1
Enter fullscreen mode Exit fullscreen mode

Great! Now, node.js-specific commands such as node and npm will be mapped to that node version. Let's celebrate by installing the yarn package manager:

npm install -g yarn
Enter fullscreen mode Exit fullscreen mode

After the installation ends, let's check if everything went OK:

yarn -v
Enter fullscreen mode Exit fullscreen mode

If you get the yarn version as output, congratulations! The set-up was properly done.

Managing Multiple NodeJS Versions

Now that we have the LTS version, what's the good of having a version manager if not to use different versions? Let's also install the most recent node version, pre-aliased as latest (currently 16.11.1):

nvm install latest
Enter fullscreen mode Exit fullscreen mode

Anytime you'd like to check your locally installed versions, run

nvm list
Enter fullscreen mode Exit fullscreen mode

to get a list of the ones available in your system. To change your current version, simply run nvm use again, this time pointing to the newly-installed one:

nvm use 16.11.1
Enter fullscreen mode Exit fullscreen mode

Note that if you run yarn -v again you will not receive a version number as yarn is not currently installed for your local 16.11.1; every installed version is completely self-contained, which includes access to global packages.

Congratulations, you are now an organized Windows NodeJS developer who follows the best practices of localized version management.

Troubleshooting Common Problems

My downloads through npm/yarn are REALLY slow.

First of all, make sure the network you are connected to is classified as 'private' by windows, as the windows firewall can be very picky on public networks. In case the problem persists, whitelist the nvm directory (should be C:\Users\<your_user_name>\AppData\Roaming\nvm if you kept the defaults) in your antivirus software.

Running stuff in node (e.g. transpiling a Typescript project) is REALLY slow.

Windows uses the NTFS filesystem which is particularly bad at dealing with tasks involving a very big number of small files and Node JS projects are notorious for the many different modules which depend on many different other modules, so this problem is harder to mitigate. Short of getting an SSD, your best bet would be to set up node.js on Windows Subsystem for Linux in case execution speed is unworkably slow.

I'm getting a 145 exit code in some NVM commands.

Take a look at the Install NVM for Windows part of this tutorial, especially the one regarding the symlink location. You must have installed NVM in a directory path with whitespaces; uninstall NVM and re-run nvm-setup.exe, this time making sure no selected paths contain whitespaces.

Conclusion

If you can install versions from the command line & switch between them (remember you'll need to have admin privileges to switch between versions), then all the rest is up to you as a javascript (or typescript) developer. If you need to install a code editor, I'd recommend Visual Studio Code for convenience, Sublime Text 3 as a lightweight alternative to VSCode or vim if you feel like you have the time and effort to learn a new skill.

Top comments (0)