node-gyp 重建一直失败

node-gyp rebuild keep failing

我试图用 npm 安装 jest。

npm install jest -g

我认为可能是版本冲突。所以我尝试配置不同版本的 python 和 MSBuild。但我仍然不断收到 node-gyp 重建失败的错误。

错误日志如下:

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

问题已解决。 不要手动安装 python 和 c++ 依赖项,而是使用 Windows 开始菜单(在 node.js 文件夹中)的“为 nodejs 安装附加工具”。 并且(针对中国开发者)不要使用淘宝npm registry,使用http://registry.npmjs.org.