SPFx tools

Node-gyp errors on Windows and WSL

I was in the process of setting up a new workstation for a project using the SharePoint Framework (SPFx) when I ran into a few node-gyp errors. I’ve encountered these kinds of problem in the past when setting up new development environments. I’m writing today’s post so that I can come here the next time I have to fix this.

TL;DR
1. Install Python 2.7
2. Set system environment variables to “C:\tools\Python27\” & “C:\tools\Python27\Scripts” (or set .npmrc – see below)
3. Install Build Tools for Visual Studio 2017
4. “npm config set msvs_version 2017” (without quotes) to set version in .npmrc
5. Run npm install to install all dependencies

Initial setup

Here is my setup. I am using node version manager for windows (nvm) so I pulled the latest LTS 14x version of node (at the time of writing this is 14.18.2. I also installed all the dependencies needed to work with SPFx.

npm install gulp-cli yo @microsoft/generator-sharepoint --global

[email protected]
[email protected]
[email protected]
@microsoft/[email protected]
Note: Also need to run gulp trust-dev-cert once we are ready to test

My environment is set so we should be good to go!
I pulled some old SPFx code I worked on a while ago locally and ran “npm install” to install all the dependencies. This is a new machine so I thought it would be fine to fire up. I immediately received node-gyp errors.

Node-gyp is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js.
https://github.com/nodejs/node-gyp

I’ve included relevant parts below as these node-gyp errors are very verbose.

npm WARN read-shrinkwrap This version of npm is compatible with lockfileVersion@1, but package-lock.json was generated for lockfileVersion@2. 
I'll try to do my best with it!

gyp ERR! find Python
gyp ERR! find Python Python is not set from command line or npm configuration
gyp ERR! find Python Python is not set from environment variable PYTHON
gyp ERR! find Python checking if "python" can be used
gyp ERR! find Python - "python" is not in PATH or produced an error
gyp ERR! find Python checking if "python2" can be used
gyp ERR! find Python - "python2" is not in PATH or produced an error
gyp ERR! find Python checking if "python3" can be used
gyp ERR! find Python - "python3" is not in PATH or produced an error
gyp ERR! find Python checking if the py launcher can be used to find Python 2
gyp ERR! find Python - "py.exe" is not in PATH or produced an error
gyp ERR! find Python checking if Python is C:\Python27\python.exe
gyp ERR! find Python - "C:\Python27\python.exe" could not be run
gyp ERR! find Python checking if Python is C:\Python37\python.exe
gyp ERR! find Python - "C:\Python37\python.exe" could not be run
gyp ERR! find Python
gyp ERR! find Python **********************************************************
gyp ERR! find Python You need to install the latest version of Python.
gyp ERR! find Python Node-gyp should be able to find and use Python. If not,
gyp ERR! find Python you can try one of the following options:
gyp ERR! find Python - Use the switch --python="C:\Path\To\python.exe"
gyp ERR! find Python   (accepted by both node-gyp and npm)
gyp ERR! find Python - Set the environment variable PYTHON
gyp ERR! find Python - Set the npm configuration variable python:
gyp ERR! find Python   npm config set python "C:\Path\To\python.exe"
gyp ERR! find Python For more information consult the documentation at:
gyp ERR! find Python https://github.com/nodejs/node-gyp#installation
gyp ERR! find Python **********************************************************
gyp ERR! find Python
gyp ERR! configure error
gyp ERR! stack Error: Could not find any Python installation to use

I took a few things from these node-gyp errors. The first was that it needed to use python and couldn’t find an installation.

Second, the version of npm is compatible with lockfileVersion@1, but the project’s package-lock.json was generated for lockfileVersion@2 which meant I had to update my version of npm. Below is a post that can help you do just that if you are using an older version of npm and run into trouble.

Install python

I fired up my browser and downloaded python. I grabbed the latest version (3.10.2 at the time of writing) which unknowingly ended up causing another problem, but we will get to that in a bit.
https://www.python.org/downloads/windows/

I installed python to my tools directory and verified that it worked. I also took this time to update npm to the latest version (8.4.1). Thinking everything was fixed, I ran ‘npm i’ again and found yet another error! Most of the error is omitted below to keep this post short.

npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | win32 | x64
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Command failed: C:\tools\Python310\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack   File "<string>", line 1
npm ERR! gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
npm ERR! gyp ERR! stack                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
npm ERR! gyp ERR! stack SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?
npm ERR! gyp ERR! stack 

Now it looked like it was complaining about python again. I did a bit of research and saw that I needed to use an older version of python so went back to the site to download the correct 2.7 version.
https://www.python.org/downloads/release/python-2718/

I installed this version in my tools folder and updated my environment variables to use this version and not 3.10.2.  Tried ‘npm i’ again. Surely it will work now!? Nope… More node-gyp errors although this one was more specific.

More node-gyp errors?

npm ERR! Warning: Missing input files:
npm ERR! D:\git\spfx\node_modules\node-expat\build\deps\libexpat\..\..\..\deps\libexpat\version.c
npm ERR! Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.
npm ERR! MSBUILD : error MSB3428: Could not load the Visual C++ component "VCBuild.exe". To fix this, 1) install the .NET Framework 2.0 SDK, 2) install Microsoft Visual Studio 2005 or 3) add the location of the component to the system path if it is installed elsewhere.  [D:\git\spfx\node_modules\node-expat\build\binding.sln]
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using [email protected]
npm ERR! gyp info using [email protected] | win32 | x64
npm ERR! gyp http GET https://nodejs.org/download/release/v14.18.2/node-v14.18.2-headers.tar.gz
npm ERR! gyp http 200 https://nodejs.org/download/release/v14.18.2/node-v14.18.2-headers.tar.gz
npm ERR! gyp http GET https://nodejs.org/download/release/v14.18.2/SHASUMS256.txt
npm ERR! gyp http GET https://nodejs.org/download/release/v14.18.2/win-x86/node.lib
npm ERR! gyp http GET https://nodejs.org/download/release/v14.18.2/win-x64/node.lib
npm ERR! gyp http 200 https://nodejs.org/download/release/v14.18.2/win-x86/node.lib
npm ERR! gyp http 200 https://nodejs.org/download/release/v14.18.2/win-x64/node.lib
npm ERR! gyp http 200 https://nodejs.org/download/release/v14.18.2/SHASUMS256.txt
npm ERR! gyp info spawn C:\tools\Python27\python.EXE
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args   'D:\\git\\spfx\\node_modules\\node-gyp\\gyp\\gyp_main.py',
npm ERR! gyp info spawn args   'binding.gyp',
npm ERR! gyp info spawn args   '-f',
npm ERR! gyp info spawn args   'msvs',
npm ERR! gyp info spawn args   '-G',
npm ERR! gyp info spawn args   'msvs_version=auto',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   'D:\\git\\spfx\\node_modules\\node-expat\\build\\config.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   'D:\\git\\spfx\\node_modules\\node-gyp\\addon.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   'C:\\Users\\user\\.node-gyp\\14.18.2\\include\\node\\common.gypi',
npm ERR! gyp info spawn args   '-Dlibrary=shared_library',
npm ERR! gyp info spawn args   '-Dvisibility=default',
npm ERR! gyp info spawn args   '-Dnode_root_dir=C:\\Users\\user\\.node-gyp\\14.18.2',
npm ERR! gyp info spawn args   '-Dnode_gyp_dir=D:\\git\\spfx\\node_modules\\node-gyp',
npm ERR! gyp info spawn args   '-Dnode_lib_file=C:\\Users\\user\\.node-gyp\\14.18.2\\<(target_arch)\\node.lib',
npm ERR! gyp info spawn args   '-Dmodule_root_dir=D:\\git\\spfx\\node_modules\\node-expat',
npm ERR! gyp info spawn args   '-Dnode_engine=v8',
npm ERR! gyp info spawn args   '--depth=.',
npm ERR! gyp info spawn args   '--no-parallel',
npm ERR! gyp info spawn args   '--generator-output',
npm ERR! gyp info spawn args   'D:\\git\\spfx\\node_modules\\node-expat\\build',
npm ERR! gyp info spawn args   '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
npm ERR! Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
npm ERR! Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
npm ERR! Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
npm ERR! Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
npm ERR! Warning: unrecognized setting VCCLCompilerTool/MultiProcessorCompilation
npm ERR! gyp info spawn C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args   'build/binding.sln',
npm ERR! gyp info spawn args   '/clp:Verbosity=minimal',
npm ERR! gyp info spawn args   '/nologo',
npm ERR! gyp info spawn args   '/p:Configuration=Release;Platform=x64'
npm ERR! gyp info spawn args ]
npm ERR! gyp ERR! build error
npm ERR! gyp ERR! stack Error: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1
npm ERR! gyp ERR! stack     at ChildProcess.onExit (D:\git\spfx\node_modules\node-gyp\lib\build.js:262:23)
npm ERR! gyp ERR! stack     at ChildProcess.emit (events.js:400:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:282:12)
npm ERR! gyp ERR! System Windows_NT 10.0.19043
npm ERR! gyp ERR! command "C:\\tools\\nodejs\\node.exe" "D:\\git\\spfx\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd D:\git\spfx\node_modules\node-expat
npm ERR! gyp ERR! node -v v14.18.2
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok

I should also mention that I have Visual Studio 2022 installed on this machine and I assumed that it would use the build tools included with this version since I added the workload ‘Desktop development with C++’ when I was setting it up. Additionally, I was still getting the error when I also noticed the version of node-gyp (v3.8.0) that was complaining. This was pretty old as they are up to 8.4.1 at the time of writing.

I decided to dig into the source folder buried in node_modules (I do not recommend this :P) and found this block of code.

node-gyp code
\node_modules\node-gyp\lib\configure.js

It was setting the default msbuild toolset to v141 which are old Visual Studio build tools (2017 I believe).

Time to download the Visual Studio 2017 build tools (15.9). Before you ask, I tried adding the toolset to Visual Studio 2022 setup by selecting the Desktop development with C++ along with MSVC v141 – VS 2017 C++ x64/x86 build tools. It didn’t appear to have an effect, so I just installed the build tools directly.

Once things were done installing, I ran ‘npm config set msvs_version 2017’ (without quotes) to set the version before running ‘npm i’.
Success!!!

After thoughts

When I first saw the error, I vaguely recalled installing some form of build tools for windows in the past. I found old instructions I previously used and saw that the package was deprecated and the site was only there for archive purposes.
https://www.npmjs.com/package/windows-build-tools

“Please note that the official Node.js for Windows installer can now automatically install the required tools. That’s likely a much better option than the module listed here (windows-build-tools).”

https://www.npmjs.com/package/windows-build-tools

I also could have set the python version in my .npmrc file instead of making them an environment variable by running the command below.

npm config set python python2.7

The user configuration for the .npmrc file is usually located at C:\Users\<Username>\.npmrc

The global configuration file in Windows is located under the etc directory in your nodejs symlink. If you have followed along with my other blog posts, it will be “C:\tools\nodejs\etc\.npmrc”

NVM and node-gyp errors on WSL

I ran into the same node-gyp errors described above when working with NVM in Linux and WSL. Here are some helpful commands.

Set the default version of node with the command below (to persist the version of node). Note this version of NVM is not the same as the one I am using in Windows.

nvm alias default 14.18.2

Update your version of NVM.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

Check where the current NVM directory is located. You should get something similar to “/home/user/.nvm/versions/node/v14.18.2/bin/node” as a result.

nvm which current

Update and install python (correct version).

sudo apt update
sudo apt upgrade
sudo apt install python2
pythonn2 -v

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.