需要 node-gyp 的 npm 安装在 Windows 上失败

npm install that requires node-gyp fails on Windows

我有一个使用 bufferutilsutf-8-validate 的 NPM 项目,两者都需要安装 node-gyp。当我执行 npm install 时,出现以下错误:

> bufferutil@1.2.1 install C:\Users\Marek\WEB\moje-skoly\web-app\node_modules\bufferutil                       
> node-gyp rebuild                                                                                             


C:\Users\Marek\WEB\moje-skoly\web-app\node_modules\bufferutil {git}{hg}                                        
{lamb} if not defined npm_config_node_gyp (node "C:\Users\Marek\AppData\Roaming\npm\node_modules\npm\bin\node-g
yp-bin\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node "" rebuild )                        
Building the projects in this solution one at a time. To enable parallel build, please add the "/m" switch.    
  bufferutil.cc                                                                                                
C:\Users\Marek\.node-gyp.1.1\include\node\v8.h(18): fatal error C1083: Cannot open include file: 'stddef.h': 
 No such file or directory [C:\Users\Marek\WEB\moje-skoly\web-app\node_modules\bufferutil\build\bufferutil.vcx 
proj]                                                                                                          
gyp ERR! build error                                                                                           
gyp ERR! stack Error: `C:\Program Files (x86)\MSBuild.0\bin\msbuild.exe` failed with exit code: 1           
gyp ERR! stack     at ChildProcess.onExit (C:\Users\Marek\AppData\Roaming\npm\node_modules\npm\node_modules\nod
e-gyp\lib\build.js:276:23)                                                                                     
gyp ERR! stack     at emitTwo (events.js:87:13)                                                                
gyp ERR! stack     at ChildProcess.emit (events.js:172:7)                                                      
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)                   
gyp ERR! System Windows_NT 10.0.10586                                                                          
gyp ERR! command "C:\Program Files\nodejs\node.exe" "C:\Users\Marek\AppData\Roaming\npm\node_modules\
npm\node_modules\node-gyp\bin\node-gyp.js" "rebuild"                                                       
gyp ERR! cwd C:\Users\Marek\WEB\moje-skoly\web-app\node_modules\bufferutil                                     
gyp ERR! node -v v5.1.1                                                                                        
gyp ERR! node-gyp -v v3.2.1                                                                                    
gyp ERR! not ok                                                                                                
npm WARN install:bufferutil@1.2.1 bufferutil@1.2.1 install: `node-gyp rebuild`                                 
npm WARN install:bufferutil@1.2.1 Exit status 1                       

之前是因为Python2.7没有安装失败,现在是这样。这让我很头疼。我该怎么办?

我在 GitHub 上找到了这个 brilliant solution:

  1. 你的 OS 必须是 Windows
  2. 通过在控制台中写入 python --version 来检查 python 是否在您的路径中。如果不是那么
  3. 下载 python 2.7(我推荐 chocolatey (choco install python2 -y))并将 python.exe 添加到您的 PATH 变量中。
  4. 你不是在 Windows 7 吗?跳过 5 和 6。
  5. Check that you have .NET 4.5.1+ installed。如果不是那么
  6. Download and install .NET 4.5.1(.NET 4.5.2 也可以正常工作)
  7. 下载Microsoft Visual C++ Build Tools 2015 Technical Preview
  8. 使用自定义安装。如果尚未安装 Windows 8.1 SDK,请安装。显然,OS 上的内容并不重要。您只需要 Windows 8.1 SDK。
  9. 将 npm 配置变量 msvs_version 设置为 2015:npm config -g set msvs_version 2015
  10. 在以 node-gyp 作为依赖项的任何项目中执行 npm i 而不会看到奇怪的错误消息

我的噩梦消失了!

下面的答案代表手动安装,但还有一种更简单的方法:自动安装。

以管理员身份打开 Powershell 并 运行 npm install -g windows-build-tools.

安装需要时间,但它对我来说就像一个魅力!

我在从旧版本的节点更新到节点 v12 时遇到了这个问题。 bcrypt 库存在一个特殊问题。我刚刚卸载了 v12 并安装了 v10,一切正常。

我在其他任何地方都找不到我的解决方案,所以想分享一下。

运行 Windows 10

上的节点 v10.16.3

安装windows-构建工具-

npm install --global --production windows-build-tools

在 C:\Users[你的用户名].npmrc 中明确设置 python 路径 - 在我的例子中,是这样的:

python=c:\users\akeel\.windows-build-tools\python27\python

在这里找到了解决方案:https://spin.atomicobject.com/2019/03/27/node-gyp-windows/

我必须安装 Windows Build Tools 2015,然后设置 Node 才能使用它。

以下是我使用过的命令:

npm install --global --production windows-build-tools --vs2015
npm config set msvs_version 2015 –global

我可以使用 WSL(windows 仅限 10 个用户)

那些不知道 WSL 是什么的人只是 Linux 命令 shell in windows

只需按照本指南安装 WSL https://wiki.ubuntu.com/WSL 并使用命令

安装 nodejs 和 npm my

sudo apt-get install nodejs npm -y

开始为我工作

对我来说没什么用(安装 msvs_version 2017、2015)

注意到了

node-gyp 错误来自 python 路径和 msvsc 路径

npm config list

输出(设置正确的安装路径和msvs)

; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.12.0 node/v12.9.0 win32 ia32"

; userconfig C:\Users\balaji\.npmrc
init-author-name = "Balaji"
init-license = "MIT"
msvs_version = "2017"
node_gyp = "C:\Users\balaji\AppData\Roaming\npm\node_modules\node-gyp\bin\node-gyp.js"
python = "C:\Users\balaji\.windows-build-tools\python27/python.exe"

; globalconfig C:\Users\balaji\AppData\Roaming\npm\etc\npmrc

; builtin config undefined
prefix = "C:\Users\balaji\AppData\Roaming\npm"

; node bin location = C:\Program Files (x86)\nodejs\node.exe
; cwd = G:\all clients project20\anware
; HOME = C:\Users\balaji
; "npm config ls -l" to show all defaults.

设置全局(例如)

npm config -g set msvs_version 2015

设置本地(例如)

npm config  set msvs_version 2015

路径修正

 npm config set python /path/to/executable/python

对于尚未安装的用户(安装 msvsc 和 python)

npm install -g windows-build-tools

安装后检查以上 givn 提示,例如路径和版本一切

快乐编码....

详细信息已更新