edge-js 构建失败,nw-gyp

问题描述

说明

我想在 nw.js 中使用 edge-js。由于 edge-js 不是纯 js 包,我必须用 nw-gyp 重建它才能让它工作。 (https://github.com/nwjs/nw.js/wiki/using-node-modules)

尝试使用 nw-gyp 重新构建 edge-js,但面临编译问题。附上错误截图

按照以下 2 个链接安装 nw-gyp https://github.com/nwjs/nw-gyp#installation

用 nw 构建 edge-js https://github.com/nwjs/nw.js/wiki/Build-native-modules-with-nw-gyp

软件包版本

"edge-js": "^15.5.2"

用于构建的命令

nw-gyp 配置 --target=0.42.3

nw-gyp 重建 --target=0.42.3

nw.js v0.42.3

错误消息和堆栈跟踪

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\vcclr.h(16): 错误:预处理指令无效 [E:\compile_node_modules\ node_modules\edge-js\build\edge_nati veclr.vcxproj]

在 ..\src\dotnet\utils.cpp:1 包含的文件中:

在 ..\src\dotnet/edge.h:22 包含的文件中:

在 C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\vcclr.h:17 包含的文件中:

在 C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\gcroot.h:42 包含的文件中:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\msclr/gcroot.h(58):错误:使用未声明的标识符“系统” [E:\compile_node_modules\node_modules\edge-js\build\edge_nativeclr.vcxproj] 在 ..\src\dotnet\utils.cpp:1 包含的文件中:

从 ..\src\dotnet/edge.h:22:C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\vcclr 包含的文件中.h(29): 错误:使用未声明的标识符“系统”[E:\compile_node_modules\node_modules\edge-js\build\edge_nativeclr.vcxproj]

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\vcclr.h(30):错误:使用未声明的标识符“系统”[E :\compile_node_modules\node_modules\edge-js\build\edge_nativeclr.vcxproj]

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\vcclr.h(31):错误:使用未声明的标识符“系统”[E :\compile_node_modules\node_modules\edge-js\build\edge_nativeclr.vcxproj]

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\vcclr.h(32):错误:使用未声明的标识符“系统”[E :\compile_node_modules\node_modules\edge-js\build\edge_nativeclr.vcxproj]

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\include\vcclr.h(44):错误:使用未声明的标识符“系统”[E :\compile_node_modules\node_modules\edge-js\build\edge_nativeclr.vcxproj]

在 ..\src\dotnet\utils.cpp:1 包含的文件中:

..\src\dotnet/edge.h(24): 错误:预处理指令无效 [E:\compile_node_modules\node_modules\edge-js\build\edge_nativeclr.vcxproj]

..\src\dotnet/edge.h(25): 错误:预处理指令无效 [E:\compile_node_modules\node_modules\edge-js\build\edge_nativeclr.vcxproj]

..\src\dotnet/edge.h(27): 错误:使用未声明的标识符“系统”[E:\compile_node_modules\node_modules\edge-js\build\edge_nativeclr.vcxproj]

相关信息

环境:Windows_NT 10.0.19041 npm 配置


\edge-js> npm config get
; cli configs
metrics-registry = "https://registry.npmjs.org/"
scope = ""
user-agent = "npm/6.13.4 node/v12.14.1 win32 x64"

; userconfig C:\Users\<user>\.npmrc
msvs_version = "2015"
node_gyp = "C:\\Users\\<user>\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js"
python = "python2.7"

; globalconfig C:\Users\<user>\AppData\Roaming\npm\etc\npmrc
node_gyp = "C:\\Users\\<user>\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js"

; builtin config undefined
prefix = "C:\\Users\\<user>\\AppData\\Roaming\\npm"

; node bin location = C:\Program Files\nodejs\node.exe
; cwd = E:\compile_node_modules\node_modules\edge-js
; HOME = C:\Users\<user>
; "npm config ls -l" to show all defaults.

解决方法

可能与 Python 3 有关。Node-gyp 最初是基于 Python 2 的,过去几年一直在过渡到 Python 3。

* 2019 - Node-gyp 5.x started adopting Python 3
* 2020 - Node-gyp 6.x started to work with Python 3
* 2020 - Node-gyp 7.x works with Python 3
* 2021 - Node-gyp 8.x removes all Python 2 support,only supports Python 3

NW-gyp 基于 Node-gyp 并进行了一些调整,回到使用 Python 2 时。它尚未更新以支持 Python 3。因此,如果您使用较新版本的 NW.js,请使用更新版本版本的 Node,并且您在全局安装了较新版本的 npm,它可能会尝试将 Python 3 的东西与 Node-gyp 一起使用,而 Node-gyp 不支持它。这完全是我的猜测,我对 Node-gyp/NW-gyp/Python 知之甚少。

如果这种预感是正确的,那么您可以尝试使用旧版本的 NW.js。 Node-gyp 5 于 2019 年 6 月 13 日开始采用 Python 3,因此在此之前的最新 node/npm 将是 Node 12.4.0 (2019-06-04) 和 npm 6.9.0。 Node 12.4.0 附带的 NW.js 版本是 v0.39.2。

  1. 如果您没有安装 nvmnvm-windows,请先卸载 Node.js 和 npm,这样您就没有全球竞争的版本。
  2. 安装 nvmnvm-windows
  3. 使用 nvm/nvm-windows 安装 Node.js 12.4.0(也应该安装 npm 6.9.0)
    • nvm install 12.4.0 && nvm use 12.4.0
  4. 删除您的node_modules
  5. npm install --save-dev nw@0.39.2-sdk
  6. npm install
  7. npm start ?‍♀️

这就是我所得到的。希望其他人可以提供更多帮助,或者这至少为您指明了正确的方向。