Windows10下React Native转TypeScript时yarn install异常
最近帮朋友排查过一模一样的场景,结合你的错误信息和尝试过的方案,给你几个针对性的解决思路:
问题回顾
你在搭建标准React Native环境并迁移到TypeScript时,执行yarn install遇到了构建错误,核心提示是:
MSBUILD : error MSB4132: The tools version "2.0" is unrecognized. Available tools versions are "12.0", "14.0", "4.0".
完整错误日志如下:
yarn install v1.3.2 [1/4] Resolving packages... [2/4] Fetching packages... info fsevents@1.1.3: The platform "win32" is incompatible with this module. info "fsevents@1.1.3" is an optional dependency and failed compatibility check. Excluding it from installation. [3/4] Linking dependencies... warning " > babel-jest@22.0.4" has unmet peer dependency "babel-core@^6.0.0 || ^7.0.0-0". [4/4] Building fresh packages... [1/1] ⢀ weak: Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch. [-/1] ⢀ waiting... [-/1] ⢀ waiting... [-/1] ⢀ waiting... warning Error running install script for optional dependency: "C:\Users\me\projects\mycrate\node_modules\weak: Command failed. Exit code: 1 Command: node-gyp rebuild Arguments: Directory: C:\Users\me\projects\mycrate\node_modules\weak Output: C:\Users\me\projects\mycrate\node_modules\weak>if not defined npm_config_node_gyp (node "C:\Program Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild ) else (node "" rebuild ) gyp info it worked if it ends with ok gyp info using node-gyp@3.6.2 gyp info using node@9.3.0 | win32 | x64 gyp info spawn C:\Python27\python.EXE gyp info spawn args [ 'C:\\Program Files\\nodejs\\node_modules\\npm\\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=auto', gyp info spawn args '-I', gyp info spawn args 'C:\\Users\\me\\projects\\mycrate\\node_modules\\weak\\build\\config.gypi', gyp info spawn args '-I', gyp info spawn args 'C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\addon.gypi', gyp info spawn args '-I', gyp info spawn args 'C:\\Users\\me\\.node-gyp\\9.3.0\\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\\me\\.node-gyp\\9.3.0', gyp info spawn args '-Dnode_gyp_dir=C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp', gyp info spawn args '-Dnode_lib_file=C:\\Users\\me\\.node-gyp\\9.3.0\\<(target_arch)\\node.lib', gyp info spawn args '-Dmodule_root_dir=C:\\Users\\me\\projects\\mycrate\\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 'C:\\Users\\me\\projects\\mycrate\\node_modules\\weak\\build', gyp info spawn args '-Goutput_dir=.' ] gyp info spawn C:\Program Files (x86)\MSBuild\14.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' ] Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch. MSBUILD : error MSB4132: The tools version "2.0" is unrecognized. Available tools versions are "12.0", "14.0", "4.0". gyp ERR! build error gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild\14.0\bin\msbuild.exe` failed with exit code: 1 gyp ERR! stack at ChildProcess.onExit (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\build.js:258:23) gyp ERR!Done in 7.68s.
你已经尝试了安装独立构建工具、全局安装windows-build-tools、设置msvs_version等方案,但都没有解决。当前项目依赖如下:
"dependencies": { "react": "16.0.0", "react-native": "0.51.0" }, "devDependencies": { "@types/jest": "^22.0.1", "@types/react": "^16.0.34", "@types/react-native": "^0.51.9", "babel-jest": "22.0.4", "babel-preset-react-native": "4.0.0", "concurrently": "^3.5.1", "jest": "22.0.4", "react-test-renderer": "16.0.0", "rimraf": "^2.6.2", "tslint": "^5.8.0", "typescript": "^2.6.2" }
解决思路与步骤
这个错误的核心原因是weak包(可选依赖)的构建配置中硬编码了MSBuild工具版本为2.0,而你的系统中只有12.0、14.0、4.0版本,导致node-gyp编译失败。可以按以下优先级尝试解决方案:
1. 跳过可选依赖安装(最快解决)
既然weak是可选依赖,项目大概率不会直接依赖它的功能,直接跳过它的安装即可:
yarn install --ignore-optional
执行这个命令后,yarn会跳过所有可选依赖的安装,直接完成核心依赖的安装,不会影响React Native项目的正常运行。
2. 修改weak包的构建配置
如果必须安装这个包,可以手动修改它的配置文件:
- 进入项目根目录下的
node_modules/weak/build/config.gypi文件 - 找到其中的
tools_version字段,将值从"2.0"改为你系统中存在的版本,比如"14.0" - 保存文件后,在项目根目录执行
yarn install或者进入node_modules/weak目录执行node-gyp rebuild
3. 升级node-gyp版本
你当前使用的node-gyp是3.6.2版本,比较老旧,对新版本MSBuild的兼容性较差。全局升级node-gyp:
npm install -g node-gyp@latest
升级完成后,删除项目的node_modules文件夹和yarn.lock文件,重新执行yarn install,新版本的node-gyp会自动适配系统中的MSBuild版本。
4. 降级Node.js版本
你当前使用的Node.js 9.3.0是非LTS版本,和React Native 0.51.0的兼容性不算最好。React Native 0.5x系列推荐使用Node.js 8.x LTS版本(比如8.17.0),降级后能减少很多依赖冲突和构建问题。可以使用nvm(Node版本管理器)来快速切换版本:
# Windows用户可使用nvm-windows,安装后执行以下命令 nvm install 8.17.0 nvm use 8.17.0
切换版本后,删除node_modules和yarn.lock,重新执行yarn install。
内容的提问来源于stack exchange,提问作者user6467981

