137

I have a project created in Visual Studio, 2013.

The project file has the following properties:

ToolsVersion="12.0", PlatformToolset = v120.

I have Visual Studio 2013 and Microsoft Build Tools 2015 installed. This project gets successfully built using MSBuild 12.0. Upon the attempt to build it with MSBuild 14.0 I get an error

MSB4019: The imported project "C:\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk. 

As I understand, the problem is that the variable VCTargetsPath doesn’t get evaluated. If I specify the variable VCTargetsPath before the build (with the value C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120), the problem is solved.

However, this approach is not very convenient in case the path is different. Is it possible to somehow solve the problem in a more optimal way?

The registry key and its value:

HKLM\Software\WOW6432Node\Microsoft\MSBuild\ToolsVersions\12.0\VCTargetsPath $([MSBuild]::ValueOrDefault('$(VCTargetsPath)','$(MSBuildExtensionsPath32)\Microsoft.Cpp\v4.0\V120\'))
Serg
  • 1,381
  • 2
  • 8
  • 5
  • *Upon the attempt to build it with MSBuild 14.0* how exactly do you do that? You should probably call vcvars.bat (or equivalent for build tools 2015) first.. – stijn Jan 17 '17 at 14:17
  • Agree with stijn, please set the Path and Environment Variables for Command-Line Builds via vcvars.bat and check if it works for you. For more information about set the Path and Environment Variables for Command-Line Builds, please refer to: https://msdn.microsoft.com/en-us/library/f2ccy3wt.aspx – Zhanglong Wu - MSFT Jan 23 '17 at 07:38
  • Possible duplicate of [Why does MSBuild look in C:\ for Microsoft.Cpp.Default.props instead of c:\Program Files (x86)\MSBuild? ( error MSB4019)](https://stackoverflow.com/questions/16092169/why-does-msbuild-look-in-c-for-microsoft-cpp-default-props-instead-of-c-progr) – Florian Winter Oct 11 '18 at 15:20

22 Answers22

77

This will usually solve the problem:

  1. Run on an elevated command prompt (admin): npm install --global --production windows-build-tools. Which requires npm to be installed (my recommended package manager).
  2. Change ownership of the install dir (in my case C:\Program Files (x86)\MSBuild\14.0) to your user, instead of admin. (properties>Security>Advanced).

Sometimes step 2 is unnecessary. It is still recommended to avoid having to use admin console for some commands.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
Nic Szerman
  • 1,834
  • 18
  • 25
71

I recently reinstalled Visual studio 2017 and encountered this issue while trying to install "npm java". The fix for me is:

  1. set ms version to 2017: npm config set msvs_version 2017
  2. set VCTargetsPath environment variable to C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\IDE\VC\VCTargets
  3. run command console as system administrator
Alex
  • 1,692
  • 13
  • 10
  • 16
    `npm config set msvs_version 2017` this code saved my hours. Thanks. – canmustu Jul 21 '19 at 00:41
  • @Alex did your `VCTargets` directory already contain `Microsoft.Cpp.Default.Props`? I have the directory but that file is missing. – starscream_disco_party Aug 21 '19 at 15:00
  • @starscream_disco_party yes, Microsoft.Cpp.Default.Props is already in my VCTargets folder – Alex Aug 22 '19 at 03:19
  • 2
    2 years later and still running into this issue. Buy a new laptop install everything and then spend days fighting this problem. the npm config set msvs_version 2017 was the fix for me – Bruce C May 13 '21 at 18:53
  • I was having issues to install npm-sass@4.13.0, after running command "npm config set msvs_version 2017" it seems to have solved the problem for me, thanks – Coder0997 Sep 23 '21 at 12:26
  • Also works with VS 2022 <3 – Mr Patience May 20 '22 at 08:46
27

These steps solved my issue:

  1. Run npm install --global --production windows-build-tools
  2. Run npm install --save nan
  3. Set VCTargetsPath environment variable to C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140
Emre Tapcı
  • 1,743
  • 17
  • 16
25

For those using VS 2019:

set VCTargetsPath="C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160"

Edit (Sep 2020): Sometimes it requires a trailing backslash (as bmiller mentioned below)

David Noreña
  • 3,951
  • 1
  • 28
  • 43
TheRennen
  • 549
  • 4
  • 16
  • 2
    This worked for me. Note that I also required a trailing backslash on the path, else the build failed looking for specific platform props in a subfolder. – bmiller Sep 02 '20 at 19:07
  • 4
    Worked for me as well, although you need to make sure you specified proper VS version (in my case it was Community - "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Microsoft\VC\v160" – g0rski Nov 11 '20 at 12:31
  • 2
    Thx, for VS2022, set VCTargetsPath=C:\Program Files\Microsoft Visual Studio\2022\Professional\Msbuild\Microsoft\VC\v170 – Lin Song Yang Jun 24 '22 at 01:21
  • Enterprise VS 2019 version can set VCTargetsPath to : C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VC\v160 – Tore Aurstad Jul 21 '23 at 11:48
15

For those the above answer does not work, here is another possible solution to look at.

Even the installation of build tools or VS 2015 did not work for me. So I tried installing below directly via PowerShell (as admin)

https://chocolatey.org/packages/visualcpp-build-tools/14.0.25420.1 Command: choco install visualcpp-build-tools --version 14.0.25420.1

Once this was installed, set an environment variable VCTargetsPath=C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\v140

Sorabh Mendiratta
  • 911
  • 12
  • 23
8

I solved this by:

  1. Set VCTargetsPath environment variable to C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140
  2. build using admin
PytLab
  • 539
  • 6
  • 12
4

When running MSBuild outside of Visual Studio 2019 (migrated from 2012), I had these kinds of problems. I solved them running this MSBuild:

"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\msbuild"

(customize it based on your VS version and target architecture) instead of this one:

C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild

that I was using when I had Visual Studio 2012.

bluish
  • 26,356
  • 27
  • 122
  • 180
3

I could not make it work with all the suggestions on this page

I downgraded nodejs to 10.15 and it worked fine

probably not the best answer but it works

phil123456
  • 1,192
  • 2
  • 10
  • 26
  • Yup Node 10 is fine for me, unfortunately our servers are on node 12 now and node 10 is being depreciated so it's off to Linux for me. – The Coder Aug 14 '21 at 00:02
1

I solved the problem by loading the solution in VS 2019 instead of double-clicking the solution name in File Explorer.

gerritvn
  • 43
  • 5
1

After calling VS2019's vcvarsall.bat, it is necessary to use VS2019's MSBuild instead of the one in C:\Windows.

set VCVARSALL=%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\VC\Auxiliary\Build\vcvarsall.bat
call "%VCVARSALL%" amd64
set MSBUILD=%ProgramFiles(x86)%\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe
rem set MSBUILD=%FrameworkDir%\%FrameworkVersion%\msbuild.exe
Steve Mitchell
  • 1,895
  • 1
  • 15
  • 12
1

My specific problem was using "yarn install" and during the build process, it was trying to build the node_modules node-sass component using MSBuild, and failing.

I would get the following error: "error MSB4019: The imported project "C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\Microsoft.Cpp.Default.props" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk."

This makes sense on a new developer machine with only Visual Studio 2022 installed. After investigating the node-sass module's build\binding.sln file, it appeared indicate that it requires Visual Studio 2015 VC++ Build Tools.

I then downloaded the "Visual C++ Build Tools for Visual Studio 2015 with Update 3" DVD image from Microsoft. After I installed it, it seemed to solve the problem, no PATH or registry changes required. When I ran "yarn install" again, building the module node-sass completed properly.

CokoBWare
  • 456
  • 3
  • 13
1

First I went to the path and confirmed that I didn't have that file. I was able to get that file by

  1. opening the Visual Studio Installer,
  2. clicking on "Modify" of the current installation
  3. selecting "C++ Clang-cl v142 build tools (x64/x86)" under the Individual Components" tab.
  4. Confirm the installation by clicking the "Modify" button
rsc
  • 10,348
  • 5
  • 39
  • 36
  • This worked for me. The Microsoft.Cpp.Default.props file was missing from the `C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VC\v160` directory, and this added it there. – mkkekkonen Dec 22 '22 at 16:42
0

I tried npm install and VCTargetsPath, all not work. Finally solved after it after upgrading visual studio to the latest version.

PaaSFly
  • 11
0

Resolved by installing Visual Studio Express 2015 for Windows 10.

Patapoom
  • 794
  • 1
  • 7
  • 16
  • Moving from Express to Pro caused the issue in my case. Set VCTargetsPath environment variable to C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140 – Sergei Krivonos Apr 28 '20 at 21:47
0

This happened for me when I used node version 14.15.3, install Node Version Manager, install lower Node version (8.x.x for example), switch to it and that should solve the issue.

Luka Devic
  • 41
  • 2
0

In my case, the reason was that the system environment variable VisualStudioVersion was pointing to the removed version of Visual Studio.

The same solution was building ok from inside the Visual Studio but failed when building with the command-line MSBuild.

The problem was solved by changing the environment variable manually.

0

I have VS 2019 and I met the same problem trying to install module 'keytar' on Windows 10. This is what I did in order to solve the problem:

  1. Opened "Visual Studio Installer" and removed extra 2017/2019 build tools (for example "Visual Studio Build Tools 2017", restarted the PC
  2. Got back into "Visual Studio Installer" / "Visual Studio Professional 2019" / "Modify" and added "Desktop development with C++" workload
  3. Got into project folder with my problem, removed node_modules folder
  4. Opened CMDO
  5. As @TheRennen suggested performed the command: set VCTargetsPath=C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Microsoft\VC\v160\ (without quotation marks and with one backslash at the end)
  6. run yarn command and now there were no errors.

I think now it a time to add VCTargetsPath into Windows env values.

node v14.19.0, npm 7.20.5, yarn 1.22.15

KEMBL
  • 536
  • 6
  • 10
0

I have faced this issue while building my flutter application. The issue arises because of the program in my case flutter was unable to locate the file "Microsoft.Cpp.Default.props" which is required for building flutter apps in windows.

For solving the issue install MicroSoft Visual Studio(not Visual Studio code) from the below url. https://visualstudio.microsoft.com/downloads/

Make sure to check the below option before starting installation.

After installation go to below directory.

C:\Program Files\Microsoft Visual Studio\2022\Community\Msbuild\Microsoft\VC

Note : I have installed community version of 2022 Visual Studio. The folder structure may be different based on your VS version

You can see few folders with version numbers

Open the folder and check which one have the "Microsoft.Cpp.Default.props" file . For my case it was the last folder v170. Open Environment variable and set "VCTargetsPath" variable with path to the folder in which "Microsoft.Cpp.Default.props" is present. Save path and restart the build process. This should work.

0

Was running project on Jetbrains Rider without Visual Studio installed (just MSBuild), and got this error.

Fixed it by downloading Visual Studio installer and installing C++ desktop development kit (just the core files - skipped testing, address sanitizer, etc.).

alexpanter
  • 1,222
  • 10
  • 25
0

For Visual Studio 2022 it helped to add Build Tools in Visual Studio Installer, and my environment variable VCTargetsPath was set to:

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\Common7\IDE\VC\VCTargets

Note the setting of the system variable VCTargetsPath in this Environment Variables screenshot:

enter image description here

skomisa
  • 16,436
  • 7
  • 61
  • 102
-1

While checking the .vcxproj file, I noticed the ToolsVersion was missing. I added it and it solves the issue.

Before :

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

After :

<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
David Buck
  • 3,752
  • 35
  • 31
  • 35
-2

Make sure to use --global with the command in order to be able to use this package anywhere. This solves the issue for me