node-gyp: node-gyp is unable to find msbuild if VS2019 is installed.

  • Node Version: node 10.15.1 npm 6.4.1
  • Platform: Windows Server 2019
  • Compiler: msbuild 15.9.21+g9802d43bc3
Verbose output (from npm or node-gyp):
Exit code: 1
Command: node-gyp rebuild
Arguments: 
Directory: D:\a\1\jest\node_modules\weak
Output:
gyp info it worked if it ends with ok
gyp info using node-gyp@3.8.0
gyp info using node@10.15.1 | win32 | x64
gyp http GET https://nodejs.org/download/release/v10.15.1/node-v10.15.1-headers.tar.gz
gyp http 200 https://nodejs.org/download/release/v10.15.1/node-v10.15.1-headers.tar.gz
gyp http GET https://nodejs.org/download/release/v10.15.1/SHASUMS256.txt
gyp http GET https://nodejs.org/download/release/v10.15.1/win-x86/node.lib
gyp http GET https://nodejs.org/download/release/v10.15.1/win-x64/node.lib
gyp http 200 https://nodejs.org/download/release/v10.15.1/SHASUMS256.txt
gyp http 200 https://nodejs.org/download/release/v10.15.1/win-x86/node.lib
gyp http 200 https://nodejs.org/download/release/v10.15.1/win-x64/node.lib
gyp info spawn C:\ProgramData\Chocolatey\bin\python2.EXE
gyp info spawn args [ 'D:\\a\\1\\jest\\node_modules\\node-gyp\\gyp\\gyp_main.py',
gyp info spawn args   'binding.gyp',
gyp info spawn args   '-f',
gyp info spawn args   'msvs',
gyp info spawn args   '-G',
gyp info spawn args   'msvs_version=2015',
gyp info spawn args   '-I',
gyp info spawn args   'D:\\a\\1\\jest\\node_modules\\weak\\build\\config.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'D:\\a\\1\\jest\\node_modules\\node-gyp\\addon.gypi',
gyp info spawn args   '-I',
gyp info spawn args   'C:\\Users\\VssAdministrator\\.node-gyp\\10.15.1\\include\\node\\common.gypi',
gyp info spawn args   '-Dlibrary=shared_library',
gyp info spawn args   '-Dvisibility=default',
gyp info spawn args   '-Dnode_root_dir=C:\\Users\\VssAdministrator\\.node-gyp\\10.15.1',
gyp info spawn args   '-Dnode_gyp_dir=D:\\a\\1\\jest\\node_modules\\node-gyp',
gyp info spawn args   '-Dnode_lib_file=C:\\Users\\VssAdministrator\\.node-gyp\\10.15.1\\<(target_arch)\\node.lib',
gyp info spawn args   '-Dmodule_root_dir=D:\\a\\1\\jest\\node_modules\\weak',
gyp info spawn args   '-Dnode_engine=v8',
gyp info spawn args   '--depth=.',
gyp info spawn args   '--no-parallel',
gyp info spawn args   '--generator-output',
gyp info spawn args   'D:\\a\\1\\jest\\node_modules\\weak\\build',
gyp info spawn args   '-Goutput_dir=.' ]
gyp info spawn C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\15.0\Bin\MSBuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args   '/clp:Verbosity=minimal',
gyp info spawn args   '/nologo',
gyp info spawn args   '/p:Configuration=Release;Platform=x64' ]

I have also tried to install VS2017 build tools on the machine in addition to VS 2019 and node-gyp still tries to find msbuild under the VS2019 install. The issue is due to VS2019 having a different location for msbuild form VS2017. It puts it under C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\Current\Bin instead of C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\MSBuild\15.0\Bin

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 46
  • Comments: 36 (4 by maintainers)

Commits related to this issue

Most upvoted comments

My solution was running this command before npm install:

npm config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe"

@zhulika @chrisrpatterson I wasn’t comfortable touching configure.js plus I needed something that I can use to fix it in our build system now, so I created this temporary workaround in PowerShell. All it does is create a shim: MSBuild\15.0\Bin\MSBuild.exe -> MSBuild\Current\Bin\MSBuild.exe. Works well for me so far.

Updated, here’s how the story continued after upgrading to node-gyp 5.

node-gyp v5.0.0 is out with Visual Studio 2019 support. It’s not yet integrated into npm so it has to be installed manually. This should do it (only need to be run once):

Windows Command Prompt
npm install --global node-gyp@latest
for /f "delims=" %P in ('npm prefix -g') do npm config set node_gyp "%P\node_modules\node-gyp\bin\node-gyp.js"

Powershell
npm install --global node-gyp@latest
npm prefix -g | % {npm config set node_gyp "$_\node_modules\node-gyp\bin\node-gyp.js"}


That is, install node-gyp globally and make the npm config variable node_gyp point to node-gyp.js.

How to undo this after node-gyp is integrated into npm
npm uninstall --global node-gyp
npm config delete node_gyp


Using yarn instead of npm fixed this for me *shrug*

I’ve just hit this, too. For those who want a temporary workaround, hand-editing the file configure.js in node-gyp appears to work. I changed line 161 from this:

variables['msbuild_path'] = path.join(vsSetup.path, 'MSBuild', '15.0',

to this:

variables['msbuild_path'] = path.join(vsSetup.path, 'MSBuild', 'Current',

then reran:

node-gyp configure

  1. Install legacy Python and set env var PYTHON to C:\Python27\python.exe
  2. npm install --g node-gyp@latest
  3. In VS’19 make sure you’ve checked “MSVC v141 - VS 2017 C++ x64/x86 build tools” under “Desktop dev with C++” and installed (You can check it in VS installer)
  4. npm config msbuild_path C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe
  5. Add “C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\MSBuild.exe” as system env variable.
  6. Now run “npm install”

Fourth point might be redundant. This worked for me, hope it works for you as well. Thanks.

I have sent PR #1715.

set path=%path%;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin

as a workaround yes. but I’ve never had MSbuild in my path before.

this seems like a really weird solution but i was able to fix it by doing the following in an administrator powershell:

cd "C:\Program Files\nodejs\node_modules\npm\node_modules"
npm i -g node-gyp@latest
npm i node-gyp@latest

had this problem on fresh installs of VS 2019 on two different laptops and this has fixed them both.

Apologies if I’m being stupid but I don’t think that this works, either. I ran the script (from a batch file called install-node-gyp-latest.cmd that I created from your post):

D:\work\gml>install-node-gyp-latest.cmd

DD:\work\gml>npm install --global node-gyp@latest
D:\Program Files\nodejs\node-gyp -> D:\Program Files\nodejs\node_modules\node-gyp\bin\node-gyp.js                          06b5.lock for D:\Program Files\nodejs\node_modules\.staging
+ node-gyp@5.0.0
updated 1 package in 6.637s

D:\work\gml>

then tried to build a native addin on a machine that has VS2019:

D:\work\gml\serenity\0.2\src\serenityNode>node-gyp build
gyp info it worked if it ends with ok
gyp info using node-gyp@5.0.0
gyp info using node@12.4.0 | win32 | x64
gyp info spawn D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\15.0\Bin\MSBuild.exe
gyp info spawn args [
gyp info spawn args   'build/binding.sln',
gyp info spawn args   '/clp:Verbosity=minimal',
gyp info spawn args   '/nologo',
gyp info spawn args   '/p:Configuration=Release;Platform=x64'
gyp info spawn args ]
gyp ERR! UNCAUGHT EXCEPTION
gyp ERR! stack Error: spawn D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\15.0\Bin\MSBuild.exe ENOENT
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
gyp ERR! stack     at onErrorNT (internal/child_process.js:456:16)
gyp ERR! stack     at processTicksAndRejections (internal/process/task_queues.js:84:9)
gyp ERR! System Windows_NT 10.0.17134
gyp ERR! command "D:\\Program Files\\nodejs\\node.exe" "D:\\Program Files\\nodejs\\node_modules\\node-gyp\\bin\\node-gyp.js" "build"
gyp ERR! cwd D:\work\gml\serenity\0.2\src\serenityNode
gyp ERR! node -v v12.4.0
gyp ERR! node-gyp -v v5.0.0
gyp ERR! This is a bug in `node-gyp`.
gyp ERR! Try to update node-gyp and file an Issue if it does not help:
gyp ERR!     <https://github.com/nodejs/node-gyp/issues>

Why is it trying to find MSBuild here:

D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\15.0\Bin\MSBuild.exe

when it should be looking here:

D:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe

?

Is there something else that I should have done (obvious to most but I’m new to this so probably not obvious to me)?

As a side note, if we open “Visual Studio cmd for vs2019” and run node-gyp from it, it will detect MSBUILD.exe correctly, but most of the created projects will require v14.1 toolset and default one shipped with vs 2019 is v14.2

error MSB8020: The build tools for Visual Studio 2017 (Platform Toolset = ‘v141’) cannot be found. To build using the v141 build tools, please install Visual Studio 2017 build tools. Alternatively, you may upgrade to the current Visual Studio tools by selecting the Project menu or right-click the solution, and then selecting “Retarget solution”

Worst issue I’ve run into in a good while. None of the above solved it for me. I had to simply use Visual Studio Installer , to modify installation to include “Desktop Development with C++” option. Simple. Then it explained an error, though it found the precise path of 2019, it was still using a blank msvs_version (despite having configured it previously per above, strange?). npm config set msvs_version "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise" Worked instantly.

@PaulGG that will cause you issues when uninstalling or updating Node.js, expect node-gyp to stop working in the future. Changing the installation under C:\Program Files\nodejs\ is always a bad idea.

The correct solution for PowerShell is:

npm install --global node-gyp@latest
npm prefix -g | % {npm config set node_gyp "$_\node_modules\node-gyp\bin\node-gyp.js"}

Guys this is fixed 2 years ago, you just need to install latest node-gyp globally and set npm to use that, detailed in a cm above:

node-gyp v5.0.0 is out with Visual Studio 2019 support. It’s not yet integrated into npm so it has to be installed manually. This should do it (only need to be run once):

Windows Command Prompt

npm install --global node-gyp@latest

for /f "delims=" %P in ('npm prefix -g') do npm config set node_gyp "%P\node_modules\node-gyp\bin\node-gyp.js"



Powershell

npm install --global node-gyp@latest

npm prefix -g | % {npm config set node_gyp "$_\node_modules\node-gyp\bin\node-gyp.js"}




That is, install node-gyp globally and make the npm config variable node_gyp point to node-gyp.js.

How to undo this after node-gyp is integrated into npm

npm uninstall --global node-gyp

npm config delete node_gyp




I’ve done everything i’ve seen on this thread but still doesn’t work, it was working before i updated node to 13.7.0 and i installed VS2019 Professional, i don’t know which of those 2 things broke it

With MSBuild being on the PATH (after running vcvars before compiling), my setup was able to find MSBuild correctly, however, I get the error as above. I ended up having to edit configure.js to use the platform toolset v142 in order to get mine to work.

My solution was running this command before npm install:

npm config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe"

Kind of dumb and late… changing \Enterprise\ to \Community\ worked for me. Should be logic since I don’t have VS Enterprise Edition 😕

Posting this, since this thread helped me, but lost some time figuring out the problem. Sometimes, on this situations, you just go blind to to little details.

@jeromesubs that command is for a cmd shell, but you’re probably getting a PowerShell terminal inside VSCode. Try this one:

npm prefix -g | % {npm config set node_gyp "$_\node_modules\node-gyp\bin\node-gyp.js"}

My solution was running this command before npm install:

npm config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\MSBuild.exe"

for others who do not use VSC Enterprise (I have build tools only) - the my path is npm config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin\MSBuild.exe"