Complete Guide: How to Resolve 'msvs_version not set' Error from Command Line or NPM Config

The msvs_version not set error usually occurs when building Node.js modules with node-gyp on Windows systems. This error indicates that the Microsoft Visual Studio version is not specified or configured properly. In this guide, we will walk you through the process of resolving this error using the command line and NPM config.

Table of Contents

  1. Prerequisites
  2. Resolve the Error Using the Command Line
  3. Resolve the Error Using NPM Config
  4. FAQs

Prerequisites

Before diving into the solutions, make sure you have the following prerequisites:

  1. Node.js and npm: You can download the latest version of Node.js and npm from the official website.
  2. Microsoft Visual Studio: Download and install the Microsoft Visual Studio Community edition or any other supported version.
  3. Windows Build Tools: Install the required build tools by running the following command in an elevated PowerShell or command prompt (Run as Administrator):
npm install --global windows-build-tools

Resolve the Error Using the Command Line

To set the msvs_version using the command line, follow these steps:

  1. Open the command prompt or PowerShell with administrative privileges.
  2. Run the following command to set the msvs_version:
npm config set msvs_version <your_visual_studio_version> --global

Replace <your_visual_studio_version> with the version of your installed Visual Studio, such as 2015, 2017, or 2019.

For example, if you have Microsoft Visual Studio 2019 installed, the command will be:

npm config set msvs_version 2019 --global

Now, try building your Node.js module again. The error should be resolved.

Resolve the Error Using NPM Config

Alternatively, you can set the msvs_version using the npm config file. To do so, follow these steps:

  1. Locate the .npmrc file in your user directory. If it does not exist, create one.
  2. Open the .npmrc file with a text editor, such as Notepad or Visual Studio Code.
  3. Add the following line at the end of the file:
msvs_version=<your_visual_studio_version>

Replace <your_visual_studio_version> with the version of your installed Visual Studio, such as 2015, 2017, or 2019.

For example, if you have Microsoft Visual Studio 2019 installed, the line will be:

msvs_version=2019
  1. Save the file and close the text editor.
  2. Try building your Node.js module again. The error should be resolved.

FAQs

Q1: What is node-gyp and why is it used?

node-gyp is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js. It bundles the gyp project used by the Chromium team and takes care of the additional steps required for compiling native Node.js modules. Read more about node-gyp.

Q2: Can I use multiple versions of Visual Studio on the same machine?

Yes, you can have multiple versions of Visual Studio installed on the same machine. However, it is recommended to use the latest version to avoid compatibility issues with newer Node.js modules.

Q3: Why do I need to install windows-build-tools?

Windows Build Tools is a package that includes all the necessary tools and configurations required to compile native Node.js modules on Windows, such as Python and the Visual C++ Build Tools. Installing this package simplifies the setup process and ensures that all dependencies are correctly installed.

Q4: Can I set the msvs_version for a specific project instead of globally?

Yes, you can set the msvs_version for a specific project by running the command without the --global flag or adding the msvs_version configuration in the project's .npmrc file.

Q5: I followed the steps, but I'm still encountering the 'msvs_version not set' error. What should I do?

If you followed the steps in this guide and still encounter the error, you might have a different issue. In this case, try the following:

  1. Ensure that your Visual Studio installation includes the required components, such as the C++ build tools and the SDK.
  2. Update your npm and Node.js to the latest versions.
  3. Check the node-gyp repository for known issues and solutions.

If you still experience issues, consider searching for help in relevant  GitHub issues.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.