网上各种帖子安装ffi,基本上到了windows build tools这里会卡住。
使用命令npm install --global --production windows-build-tools
安装报错信息如下:
PS E:\codes\nodejsPath\tcpTest> npm install --global --production windows-build-tools
npm WARN config production Use `--omit=dev` instead.
npm WARN deprecated [email protected]: Node.js now includes build tools for Windows. You probably no longer need this tool. See https://github.com/felixrieseberg/windows-build-tools for details.
npm WARN deprecated [email protected]: this library is no longer supported
npm WARN deprecated [email protected]: Please upgrade to version 7 or higher. Older versions may use Math.random() in certain circumstances, which is known to be problematic. See c. See https://v8.dev/blog/math-random for details.
npm WARN deprecated [email protected]: request has been deprecated, see https://github.com/request/request/issues/3142
npm ERR! code 1
npm ERR! path C:\Users\aaaa\AppData\Roaming\npm\node_modules\windows-build-tools
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node ./dist/index.js
Downloading python-2.7.15.amd64.msi
npm ERR! [============================================>] 100.0% (0 B/s)
npm ERR! Downloaded python-2.7.15.amd64.msi. Saved to C:\Users\aaaa\.windows-build-tools\python-2.7.15.amd64.msi.
Downloading vs_BuildTools.exe
npm ERR! [============================================>] 100.0% (0 B/s)
npm ERR! Downloaded vs_BuildTools.exe. Saved to C:\Users\aaaa\.windows-build-tools\vs_BuildTools.exe.
npm ERR!
npm ERR! Starting installation...
npm ERR! Downloading installers failed. Error: TypeError: 'process.env' only accepts a configurable, writable, and enumerable data descriptor
npm ERR! at Function.defineProperty ()
npm ERR! at Object.removePath (C:\Users\aaaa\AppData\Roaming\npm\node_modules\windows-build-tools\dist\utils\remove-path.js:11:12)
npm ERR! at Object.install (C:\Users\aaaa\AppData\Roaming\npm\node_modules\windows-build-tools\dist\install\index.js:29:19)
npm ERR! at C:\Users\aaaa\AppData\Roaming\npm\node_modules\windows-build-tools\dist\start.js:17:19
npm ERR! at Object.download (C:\Users\aaaa\AppData\Roaming\npm\node_modules\windows-build-tools\dist\download.js:35:5)
npm ERR! at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
npm ERR! at async Object.aquireInstallers (C:\Users\aaaa\AppData\Roaming\npm\node_modules\windows-build-tools\dist\aquire-installers.js:32:13) {
npm ERR! code: 'ERR_INVALID_OBJECT_DEFINE_PROPERTY'
npm ERR! }
npm ERR! windows-build-tools will now exit.npm ERR! A complete log of this run can be found in: C:\Users\aaaa\AppData\Local\npm-cache\_logs\2023-08-14T13_44_54_835Z-debug-0.log
PS E:\codes\nodejsPath\tcpTest>
ffi要正常工作的根本原理是需要正确的python版本和c++编译的tools
经过反复折腾,开发环境很容易被整的乱七八糟。所以先清理干净环境再用命令去跑。
这个错误尝试降低nodejs版本。我是用的是当前最新的版本node-v18.17.1,可能存在版本的不兼容或者bug之类的问题。我降低到node-v17.9.1-x64
另外电脑里不能有python的其他版本,清理干净。包括环境变量。如果在报错之前还做了大量尝试,最好把这些缓存都清理干净。包括c盘用户目录下,一般会生成一个node的环境配置文件
C:\Users\aaaa\.node_repl_history
C:\Users\aaaa\AppData\Local\npm-cache
C:\Users\aaaa\AppData\Roaming\npm
这些需要删掉,避免环境配置下次安装好又读取进来了,或者因为看了各种帖子,操作乱了。
前面准备好后,开始安装node-v17.9.1-x64,安好后,先确认npm环境,看看是否干净如初,没有被自己改的乱七八糟的配置
npm config get
npm install -g node-gyp
npm install --global --production windows-build-tools
它会一直卡在这里。按ctrl+c,强制终止,看到报错信息如下
原来是它安装到Visual Studio Build Tools的时候开始没反应了
根据日志前面提到的downloaded路径去看,的确有成功下载visual studio build tools。由于本人电脑有安装更高版本的vs开发工具,我怀疑是有冲突或者在下载什么vs的工具包,里面报错没有反馈出来。于是我打开下载好的文件夹,手动安装visual studio build tools
手动安装能成功
但是,即便如此,还是不能正常npm install ffi。后面查了些资料,有一个说的特别明白。因为ffi用到了v8的一些接口,v8后面接口改了。所以直接用ffi跑不起来。
npm install ffi命令换成npm i ffi-napi。说是ffi-napi做了修正。
再项目文件夹下执行命令,安装成功。
js代码如下:
const ffi = require("ffi-napi");
const libm = ffi.Library("test.dll", {
"Add": ["int", ["int","int"]]
});
console.log(libm.Add(1,5));
用go语言写了一个简单的测试代码,生成.h头文件和.dll文件
//go语言代码,可以用c++,c#等自行实现
package main
import "C"
//export Add
func Add(numberA int, numberB int) int {
return numberA + numberB
}
func main() {
}