Multiple Node.js Installs with NVM and Global Packages

Explore the power of the Node Version Manager (NVM) in managing multiple Node.js installs for diverse SharePoint and Teams Toolkit projects.

By Last Updated: July 14, 2024 3 minutes read

Different SharePoint Framework (SPFx) and Microsoft Teams Toolkit (TTK) project types support various versions of Node.js. For instance, different SPFx versions require specific Node.js versions. This means you need the right version of Node.js installed on your machine to run and compile these projects.

The challenge arises when you need multiple versions of Node.js on your machine. By default, Node.js doesn’t support this; it allows only one installation. However, using the popular tool Node Version Manager (NVM), available for both Mac OS and Windows, you can install multiple Node.js versions on your computer. This tool also simplifies switching between versions on the fly.

How NVM works

When you switch to different versions of Node.js, NVM updates the PATH environment variable on your system. This change points to the folder containing the version of Node.js you wish to use.

The PATH environment variable, found on both Mac OS and Windows, serves a very important role. It specifies the directories to search when finding a command, meaning directories in the PATH allow you to run executables from those folders anywhere in your environment. In this case, you can execute the Node process from any location in your environment.

NVM makes all Node.js installs 100% isolated from each other

It’s important to note that each version of Node.js you install is completely isolated from the others. This isolation applies not only to the Node.js runtime executable, but also to the global package registry for each Node.js version.

For instance, if I have Node.js v16.20.2 installed with all necessary tools for the SPFx in the global registry - like Yeoman (yo), the Yeoman generator for SPFx (@microsoft/generator-sharepoint), and the Gulp CLI (gulp-cli) - these tools are only available for that specific Node.js version.

> node -v
v16.20.2

// show contents
> npm list -g
/Users/ac/.nvm/versions/node/v16.20.2/lib
├── @microsoft/[email protected]
├── [email protected]
├── [email protected]
├── [email protected]
└── [email protected]

If I install a new version of Node.js v18.20.2 using NVM, the global registry of that installed version won’t contain the three packages that I need.

> node -v
v18.20.2
// show contents
> npm list -g
/Users/ac/.nvm/versions/node/v18.20.2/lib
├── [email protected]
└── [email protected]

I’ll need to reinstall those three packages into the same registry.

NVM for Mac OS simplifies new Node.js version installs

One way to simplify the process is by using the Mac OS version of NVM. It contains the --reinstall-packages-from argument, which automatically installs all the same packages from another installed Node.js version to your new one. This argument can be used with the nvm install command.

nvm install v18.20.1 --reinstall-packages-from=16.20.2

This will enable NVM to automate package handling after installing a new version of Node.js. It will automatically locate all packages installed in other versions of Node.js package registry and install them into the new version right away. This greatly simplifies and accelerates the process of installing a new version of Node.js.