NPM/YARN - solving MSBUILD error and missing Microsoft.Cpp. Default.props

Share on Facebook
Share on Twitter
One of the most vital Node.js packages is called node-gyp which is "a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js". There are some important packages that depend on node-gyp (especially node-sass) and the chances are that you'll have it in the project's dependencies sooner or later. Although the aim of the tool is to account for the cross-platform bundling diversities, the installation itself may turn out to be rather problematic and the setup as well as troubleshooting differs on Unix systems, macOS and Windows.
Share on Facebook
Share on Twitter

In this tutorial, we'll take a look how to tell that the installation of this package is the problem and, importantly, also how to deal with it. For most, the error one encounters will complain either about MSBUILD version:


// npm install

Files\nodejs\node_modules\npm\bin\node-gyp-bin....\node_modules\node-gyp\bin\node-gyp.js" rebuild
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
MSBUILD : error MSB4132: The tools version "2.0" is unrecognized. Available tools versions are "4.0".

        

Or that in can't find the Python executable:


// npm install

gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
        

Luckily, both problems have a common solution.

1 Python and MSBUILD errors

Troubleshooting on Windows

There is a package called windows-build-tools which is a build tool for compiling native Node modules. It installs all the necessary tools and sets the required configuration which, trust me, saves you lots of days of cursing.

In order to install the package, run PowerShell as administrator and run the following code.


// PowerShell as administrator
npm install --global --production windows-build-tools

// Then for npm
npm config set msvs_version 2015 --global

// Or then for YARN
yarn config set msvs_version 2015 --global

        

In many cases, this is enough. Try to re-run the installation in your project, using a regular command line again (not needed to be an administrator anymore). If different yet some errors arise again, keep on reading.

2 Windows build tools

Microsoft.Cpp.Default.props was not found

This is a possible follow-up. In other words, this error is saying that the compiler could not find Microsoft Visuals' builder. There may be several reasons for that:

  • The Microsoft Visual Studio (or its variations) is not installed
  • A very old version of the The Microsoft Visual Studio is installed
  • The VCTargetsPath is not properly configured

If you don't have the visual studio installed and the installation of the windows-build-tools did not help, the problem is most likely in improper registry records. First of all, locate the Cpp folder which is usually in one of following directories:


// Locating the Microsft.cpp folder
For Visual studio 2015: drive:\Program Files (x86)\MSBuild\Microsoft.Cpp (x86)\v4.0\v140\
For Visual studio 2017: drive:\Program Files (x86)\Microsoft Visual Studio\year\edition\Common7\IDE\VC\VCTargets\

        

If it's missing, we'll try to force the installation/update of the Microsoft Visual studio manually through the windows build tools package. Locate the package (usually C:\Users\%USERNAME%\.windows-build-tools) and run the vs_BuildTools.exe inside. If you can update or install the studio, do so. Then check again if the the Microsoft.cpp folder exists.

3 Sources of the issue

Configuring VCTargetsPath

We're almost there. Now we need to tell the MSBUILD to use default props in the located folder. To do so, we'll need to edit or create the VCTargetsPath variable in registries. Do the following:

  • Type regedit in windows search
  • Navigate to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSBuild\ToolsVersions\4.0
  • Create/update string value key VCTargetsPath and set it to $(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\

That's it. The following steps have been enough to deal with the node-gyp and node-sass installation issues. I do hope this guide has resolved your problems as well.

4 Solution through registries
< back