Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
chrispat opened this issue Feb 7, 2019 · 36 comments
Closed

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

chrispat opened this issue Feb 7, 2019 · 36 comments
Labels

Comments

@chrispat
Copy link

chrispat commented Feb 7, 2019

  • 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

@chrispat chrispat changed the title node-gyp is unable to find msbuild if only VS2019 installed. node-gyp is unable to find msbuild if VS2019 is installed. Feb 7, 2019
@zhulika
Copy link

zhulika commented Apr 3, 2019

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

@noseratio
Copy link

@zhulika could you maybe submit a PR for this?

@zhulika
Copy link

zhulika commented Apr 8, 2019

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.

@noseratio
Copy link

noseratio commented Apr 8, 2019

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

syohex added a commit to syohex/node-gyp that referenced this issue Apr 9, 2019
Path of VS2019's MSBuild.exe is different from VS2017's MSBuild.exe.
This change supports its path. And improve error check for finding
msbuild.exe, if it is not found, then throws error.

Fixes: nodejs#1663
@syohex
Copy link

syohex commented Apr 9, 2019

I have sent PR #1715.

@tavrez
Copy link

tavrez commented Apr 29, 2019

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"

@SirWaddles
Copy link

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.

@d3x0r
Copy link

d3x0r commented May 7, 2019

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.

@J-Cake
Copy link

J-Cake commented May 17, 2019

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

@Zerowalker
Copy link

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

@rezonant
Copy link

rezonant commented Jun 3, 2019

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

@richardlau
Copy link
Member

Being addressed in #1762.

@joaocgreis
Copy link
Member

joaocgreis commented Jun 14, 2019

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

@zhulika
Copy link

zhulika commented Jun 17, 2019

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)?

@zhulika
Copy link

zhulika commented Jun 17, 2019

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.

@jeromesubs
Copy link

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 node-gyp@3.8.0"

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

@joaocgreis
Copy link
Member

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

@therealpaulgg
Copy link

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.

@joaocgreis
Copy link
Member

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

@noseratio
Copy link

noseratio commented Sep 12, 2019

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

@osmanraifgunes
Copy link

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"

@remjx
Copy link

remjx commented Nov 20, 2019

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

@rinme
Copy link

rinme commented Nov 30, 2019

2019-11-30T10_41_01_335Z-debug.log
Pls how to fix this

@noseratio
Copy link

@zeeterv, try this.

@jorge-agra
Copy link

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.

@soapdog
Copy link

soapdog commented Jan 23, 2020

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.

@Asaduji
Copy link

Asaduji commented Jan 28, 2020

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

@Asaduji
Copy link

Asaduji commented Jan 28, 2020

npm -v

6.13.6

@scual
Copy link

scual commented Feb 19, 2020

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"

@jayasundeep
Copy link

jayasundeep commented May 11, 2020

  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.

@ensemblebd
Copy link

ensemblebd commented Oct 13, 2020

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.

@dkocich
Copy link

dkocich commented Dec 22, 2020

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"

@cclauss cclauss added the ERR! node-gyp -v < v10.x.x npm install -g npm label Dec 22, 2020
@ariccio
Copy link

ariccio commented Feb 9, 2021

Still a problem in 2021. I wish there was a way to backport compatibility shims, so the ancient version of node-gyp in my dependencies would just work without futzing with the path vars.

@dkocich
Copy link

dkocich commented Feb 9, 2021

@ariccio if I remember correctly, I think that I ended unsetting anything in the NPM config and used Visual studio installer to add MS build tools v2017 to my system next to the existing v2019...

@tavrez
Copy link

tavrez commented Feb 9, 2021

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



@JohannWNel
Copy link

As a note to the above comment, it only worked for me when I also ran the following command:

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

@syohex Could future comments be blocked for this issue since the solutions are provided.

@nodejs nodejs locked as resolved and limited conversation to collaborators Jan 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests