1. 简介

在 Windows 10 下 npm install 失败,并报错:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
gyp WARN install got an error, rolling back install
gyp ERR! configure error
gyp ERR! stack Error: connect ETIMEDOUT 104.20.22.46:443
gyp ERR! stack at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16)
gyp ERR! System Windows_NT 10.0.19640
.....
gyp ERR! node -v v12.17.0
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! node-expat@2.3.18 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the node-expat@2.3.18 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

2. 解决

2.1 可能一

删除目录下的 node_modules 文件夹,再次 npm install 即可。

  • 原因:如果直接是从别处复制过来的项目,项目已有的 node_modules 文件夹,因此会造成兼容性冲突。

2.2 可能二

参见 github 上关于解决 node-gyp rebuild方案

  • Option 1
    Install all the required tools and configurations using Microsoft’s windows-build-tools using npm install --global --production windows-build-tools from an elevated PowerShell or CMD.exe (run as Administrator).

  • Option 2
    Install tools and configuration manually:
    Install Visual C++ Build Environment: Visual Studio Build Tools (using “Visual C++ build tools” workload) or Visual Studio 2017 Community (using the “Desktop development with C++” workload)
    Launch cmd, npm config set msvs_version 2017