I was trying to install jest with npm.
npm install jest -g
I thought there may be a version conflict. So I tried to config different versions of python and MSBuild. But still I keep getting the error that node-gyp rebuild failed.
The error log is like below:
C:\WINDOWS\system32>npm i jest -g
npm ERR! code 1
npm ERR! path C:\Users\zhoul\AppData\Roaming\npm\node_modules\jest\node_modules\canvas
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild
npm ERR! �ڴ˽��������һ������һ����Ŀ����Ҫ���ò������ɣ������ӡ�-m�����ء�
npm ERR! Backend.cc
npm ERR! C:\Users\zhoul\AppData\Local\node-gyp\Cache\16.6.1\include\node\node.h(63,10): fatal error C1083: �������ļ�: ��v8.h��: No such file or directory [C
:\Users\zhoul\AppData\Roaming\npm\node_modules\jest\node_modules\canvas\build\canvas.vcxproj]
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@7.1.2
npm ERR! gyp info using node@16.6.1 | win32 | x64
npm ERR! gyp info find Python using Python version 3.9.6 found at "C:\Program Files\Python39\python.exe"
npm ERR! gyp info find VS using VS2019 (16.11.31613.86) found at:
npm ERR! gyp info find VS "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community"
npm ERR! gyp info find VS run with --verbose for detailed information
npm ERR! (node:18044) [DEP0150] DeprecationWarning: Setting process.config is deprecated. In the future the property will be read-only.
npm ERR! (Use `node --trace-deprecation ...` to show where the warning was created)
npm ERR! gyp info spawn C:\Program Files\Python39\python.exe
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args 'C:\\Users\\zhoul\\AppData\\Roaming\\npm\\node_modules\\npm\\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 '-I',
npm ERR! gyp info spawn args 'C:\\Users\\zhoul\\AppData\\Roaming\\npm\\node_modules\\jest\\node_modules\\canvas\\build\\config.gypi',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args 'C:\\Users\\zhoul\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\addon.gypi',
npm ERR! gyp info spawn args '-I',
npm ERR! gyp info spawn args 'C:\\Users\\zhoul\\AppData\\Local\\\\node-gyp\\\\Cache\\\\16.6.1\\\\<(target_arch)\\\\node.lib',
npm ERR! gyp info spawn args '-Dmodule_root_dir=C:\\Users\\zhoul\\AppData\\Roaming\\npm\\node_modules\\jest\\node_modules\\canvas',
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 'C:\\Users\\zhoul\\AppData\\Roaming\\npm\\node_modules\\jest\\node_modules\\canvas\\build',
npm ERR! gyp info spawn args '-Goutput_dir=.'
npm ERR! gyp info spawn args ]
npm ERR! gyp info spawn C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\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:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe` failed with exit code: 1
npm ERR! gyp ERR! stack at ChildProcess.onExit (C:\Users\zhoul\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\lib\build.js:194:23)
npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:394:28)
npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
npm ERR! gyp ERR! System Windows_NT 10.0.19042
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\zhoul\\AppData\\Roaming\\npm\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd C:\Users\zhoul\AppData\Roaming\npm\node_modules\jest\node_modules\canvas
npm ERR! gyp ERR! node -v v16.6.1
npm ERR! gyp ERR! node-gyp -v v7.1.2
npm ERR! gyp ERR! not ok
Problem solved. Instead of manually installing python and c++ dependency, use "install additional tools for nodejs" from the Windows Start menu(in node.js folder). And (for Chinese developers) do not use taobao npm registry, use http://registry.npmjs.org.