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

Created on 7 Feb 2019  路  32Comments  路  Source: nodejs/node-gyp

  • 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 [email protected]
gyp info using [email protected] | 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

ERR! node-gyp -v <= v5.1.0 Windows

Most helpful comment

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"

All 32 comments

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

@zhulika could you maybe submit a PR for this?

I'm a C/C++ programmer. I'm not confident enough with Javascript or node-gyp to be able to implement something that detects the version of VS that is installed and adjust the output accordingly.

@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.

I have sent PR #1715.

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"

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.

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.

I've edited the configure.js file on line 161 to 'Current' rather than '15.0' and I still get the error with a path pointing to '15.0'.

This still appears to be a problem, and the older Visual Studio versions aren't available anymore as far as i can tell:(

Older versions of VS are still available. Scroll down to Older Versions on this page (https://visualstudio.microsoft.com/downloads/), it leads to: https://visualstudio.microsoft.com/vs/older-downloads/

Or use Chocolatey, a package manager for Windows based on Nuget. Cheers, and hope this bug is resolved soon

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


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
+ [email protected]
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 [email protected]
gyp info using [email protected] | 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)?

I think that the problem was down to my PC. I had so many node-gyp installations plastered all over this PC and it seems that it was using a copy that it shouldn't have. I have eliminated all of the obsolete versions and it is now building OK with your script applied.

Apologies for the false alawm.

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:

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"

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

Hi,
I'm a bit new here and having the same issue ...

I ran the command "npm install --global node-gyp@latest", which installed v5.0.2, as recommanded
But when I run "npm install --verbose", I get "gyp info using [email protected]"

So i guess I need to run
for /f "delims=" %P in ('npm prefix -g') do npm config set node_gyp "%P\node_modules\node-gyp\bin\node-gyp.js"
but where/how do I run such a command?

I'm developing with VsCode, and if I run this command in the console, I get an error ...

@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"}

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.

@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"}

In my case, here's what helped. In a nutshell, this PowerShell 6 inliner:

pwsh -Command npm config set node_gyp "`u{22}$(Join-Path $(npm root -g) -ChildPath 'node-gyp\bin\node-gyp.js')`u{22}"

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"

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

@zeeterv, try this.

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.

I've set all of those values and the items are installed in the correct referenced path but it still doesn't work.

my config is:

~
; userconfig C:\Users\andre.npmrc
msbuild_path = "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\MSBuild.exe"
msvs_version = "2017"
node_gyp = "C:\Users\andre\scoop\persist\nodejs-lts\bin\node_modules\node-gyp\bin\node-gyp.js"
~

If I switch that to the 2019 version it also fails even though it is also installed.

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

npm -v

6.13.6

Not sure if this has been suggested before, if so apologies. It's not a fix, but will do the job
Simply use the Visual C++ toolset for the command-line shipped with VS2017.

From the terminal you can launch vcvars64.bat (or vcvars32.bat ) file and then keep using the terminal to build node-gyp:

"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
  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\ProfessionalMSBuild\Current\Bin\MSBuild.exe
  5. Add "C:\Program Files (x86)\Microsoft Visual Studio\2019\ProfessionalMSBuild\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.

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.

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"

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lopezdp picture lopezdp  路  3Comments

kimown picture kimown  路  3Comments

jhermsmeier picture jhermsmeier  路  3Comments

Yamakaky picture Yamakaky  路  3Comments

halkar picture halkar  路  4Comments