无法安装@types/enzyme-adapter-react-16

Cannot install @types/enzyme-adapter-react-16

我需要为 React 应用程序创建单元测试。我是 React 的新手,所以我在设置方面遇到了麻烦。

我的密码是

import React from 'react';
import Enzyme, {shallow, mount} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

VS 代码说“找不到模块 'enzyme-adapter-react-16' 的声明文件”并建议 运行 npm i --save-dev @types/enzyme-adapter-react-16。 我这样做了,但出现了这个错误:

npm ERR! code 1
npm ERR! path C:\WORK_2\GIT\XXX\node_modules\node-sass
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@3.8.0
npm ERR! gyp info using node@10.19.0 | win32 | x64
npm ERR! gyp ERR! configure error 
npm ERR! gyp ERR! stack Error: Command failed: C:\Program Files\Python39\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: invalid syntax
npm ERR! gyp ERR! stack
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (child_process.js:294:12)
npm ERR! gyp ERR! stack     at ChildProcess.emit (events.js:198:13)
npm ERR! gyp ERR! stack     at maybeClose (internal/child_process.js:982:16)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
npm ERR! gyp ERR! System Windows_NT 10.0.19042
npm ERR! gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\WORK_2\GIT\XXX\node_modules\node-gyp\bin\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd C:\WORK_2\GIT\XXX\node_modules\node-sass
npm ERR! gyp ERR! node -v v10.19.0
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok

编辑:我通过安装 Python 2.7 修复了这个错误,但安装了另一个:

npm ERR! code 7
npm ERR! path C:\WORK_2\GIT\XXX\node_modules\node-sass
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@3.8.0
npm ERR! gyp info using node@10.19.0 | win32 | x64
npm ERR! gyp http GET https://nodejs.org/download/release/v10.19.0/node-v10.19.0-headers.tar.gz
npm ERR! gyp http 200 https://nodejs.org/download/release/v10.19.0/node-v10.19.0-headers.tar.gz
npm ERR! gyp http GET https://nodejs.org/download/release/v10.19.0/SHASUMS256.txt
npm ERR! gyp http GET https://nodejs.org/download/release/v10.19.0/win-x64/node.lib
npm ERR! gyp http GET https://nodejs.org/download/release/v10.19.0/win-x86/node.lib
npm ERR! gyp http 200 https://nodejs.org/download/release/v10.19.0/SHASUMS256.txt
npm ERR! gyp http 200 https://nodejs.org/download/release/v10.19.0/win-x64/node.lib
npm ERR! gyp http 200 https://nodejs.org/download/release/v10.19.0/win-x86/node.lib
npm ERR! gyp info spawn C:\Python27\python.exe
npm ERR! gyp info spawn args [ 'C:\WORK_2\GIT\XXX\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=2015',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   'C:\WORK_2\GIT\XXX\node_modules\node-sass\build\config.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   'C:\WORK_2\GIT\XXX\node_modules\node-gyp\addon.gypi',
npm ERR! gyp info spawn args   '-I',
npm ERR! gyp info spawn args   'C:\Users\XXX\.node-gyp\10.19.0\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\XXX\.node-gyp\10.19.0',
npm ERR! gyp info spawn args   '-Dnode_gyp_dir=C:\WORK_2\GIT\XXX\node_modules\node-gyp',
npm ERR! gyp info spawn args   '-Dnode_lib_file=C:\Users\XXX\.node-gyp\10.19.0\<(target_arch)\node.lib',
npm ERR! gyp info spawn args   '-Dmodule_root_dir=C:\WORK_2\GIT\XXX\node_modules\node-sass',
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:\WORK_2\GIT\XXX\node_modules\node-sass\build',
npm ERR! gyp info spawn args   '-Goutput_dir=.' ]
npm ERR! gyp info spawn C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild.0\Bin\MSBuild.exe
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 ERR! UNCAUGHT EXCEPTION
npm ERR! gyp ERR! stack Error: spawn C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild.0\Bin\MSBuild.exe ENOENT
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
npm ERR! gyp ERR! stack     at onErrorNT (internal/child_process.js:415:16)
npm ERR! gyp ERR! stack     at process._tickCallback (internal/process/next_tick.js:63:19)
npm ERR! gyp ERR! System Windows_NT 10.0.19042
npm ERR! gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\WORK_2\GIT\XXX\node_modules\node-gyp\bin\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd C:\WORK_2\GIT\XXX\node_modules\node-sass
npm ERR! gyp ERR! node -v v10.19.0
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! This is a bug in `node-gyp`.
npm ERR! gyp ERR! Try to update node-gyp and file an Issue if it does not help:
npm ERR! gyp ERR!     <https://github.com/nodejs/node-gyp/issues>

终于自己解决了,需要先运行

npm rebuild node-sass

然后:

@types/enzyme-adapter-react-16

不确定为什么会发生这种情况,似乎项目使用 node-sass 进行某些操作并导致问题。