nodejs调dll,环境安装报错处理node-gyp

24 篇文章 0 订阅
13 篇文章 1 订阅

nodejs调用c++生成的动态库dll例子,要求node >= 15.0.0
不懂的可以微信我号yizheng369

源码地址 https://gitee.com/618859/nodejs_dll

1.环境总结

1.要安装Visual Studio 2019
2.node v16.14.0或以上
3.安装python-2.7.15,并配置他的环境变量
4.全局安装node-gyp v9.0.0
5.全局安装最新的windows-build-tools
6.最后在项目里安装最新的ffi-napi
以上6步都完成后,就可以用node调用dll文件了

环境安装

  1. 安装 Visual Studio 2019 下载地址https://my.visualstudio.com/Downloads?q=Visual%20Studio%202019

install

npm install

run

node ffi.js

详细安装过程

PS D:\pratDemo\node_demo\node_dll> node -v
v16.14.0
PS D:\pratDemo\node_demo\node_dll> npm -v
8.3.1
PS D:\pratDemo\node_demo\node_dll> npm i
npm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile
npm ERR! code 1
npm ERR! path D:\pratDemo\node_demo\node_dll\node_modules\ffi-napi
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node-gyp-build
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@8.4.1
npm ERR! gyp info using node@16.14.0 | win32 | x64
npm ERR! gyp info find Python using Python version 3.10.4 found at "C:\Users\80455\AppData\Local\Programs\Python\Python310\python.exe"
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt

上面很明显是安装失败了,但下面有很多提示信息,我们耐心看

npm ERR! gyp ERR! find VS checking VS2019 (16.11.32413.511) found at: 

提示1:这里提示我们要安装 VS2019,就是 Visual Studio 2019
因为我们用nodejs调用dll时,需要VS2019的一些插件来帮助才能实现,故要安装。你也可以安装 Visual Studio 2013 或者Visual Studio 2015都行

npm ERR! gyp ERR! find VS "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community"
npm ERR! gyp ERR! find VS - found "Visual Studio C++ core features"
npm ERR! gyp ERR! find VS - missing any VC++ toolset
npm ERR! gyp ERR! find VS could not find a version of Visual Studio 2017 or newer to use
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:

提示2:你可以查看这个网址,但是我打不开

npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows 

这是一段中间的报错信息

npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use
npm ERR! gyp ERR! stack     at VisualStudioFinder.fail (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:122:47)
npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:75:16
npm ERR! gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:363:14)
npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:71:14
npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:384:16
npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.js:54:7
npm ERR! gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\util.js:33:16
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:406:5)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:520:28)
npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1092:16)
npm ERR! gyp ERR! System Windows_NT 10.0.22000
npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd D:\pratDemo\node_demo\node_dll\node_modules\ffi-napi
npm ERR! gyp ERR! node -v v16.14.0
npm ERR! gyp ERR! node-gyp -v v8.4.1
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\80455\AppData\Local\npm-cache\_logs\2022-05-25T13_35_06_797Z-debug-0.log

提示3:以上是报错信息,但从报错信息里面,我们能看到一些头绪
后来我直接百度,在百度里搜索: https://github.com/nodejs/node-gyp
搜到这篇文章:https://www.jianshu.com/p/7148b27eb61e

其实我之前成功安装过一次,我知道要安装 windows-build-tools
刚好上面文章的那个网友也说到要安装,那就理由比较充分了,那就安装嘛。
所以我就输入 npm i -g windows-build-tools

PS D:\pratDemo\node_demo\node_dll> npm i -g windows-build-tools
npm WARN deprecated har-validator@5.1.5: this library is no longer supported
npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm WARN deprecated windows-build-tools@5.2.2: 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 ERR! code 3221225786
npm ERR! path C:\Users\80455\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

**提示4:**报错信息里面有:这一句话非常经典,他提示我们需要安装python 2.7.15版本

Downloading python-2.7.15.amd64.msi  

于是我就去python官网下载 https://www.python.org/downloads/
最后我挑了python 2.7.5这个版本,也适合了,就这个地址:https://www.python.org/downloads/release/python-275/
下载好直接双击安装就好
安装过程中,默认有一个安装地址是 C:\Python27
那你不要修改,一直点击下一步就好

等python 2.7.5安装好后,你要为python配置环境变量

配置环境变量文本教程:https://blog.csdn.net/yuan_618859/article/details/124975498?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22124975498%22%2C%22source%22%3A%22yuan_618859%22%7D&ctrtid=SyFIz

配置环境变量视频教程:https://www.bilibili.com/video/BV1WE411v7mN/

接下来:

npm ERR! [============================================>] 100.0% of 20.2 MB (653 kB/s)
npm ERR! Downloaded python-2.7.15.amd64.msi. Saved to C:\Users\80455\.windows-build-tools\python-2.7.15.amd64.msi.
Downloading vs_BuildTools.exe
npm ERR! [============================================>] 100.0% of 1.12 MB (299 kB/s)
npm ERR! Downloaded vs_BuildTools.exe. Saved to C:\Users\80455\.windows-build-tools\vs_BuildTools.exe.
npm ERR!
npm ERR! Starting installation...
npm ERR! Launched installers, now waiting for them to finish.
npm ERR! This will likely take some time - please be patient!
npm ERR!
npm ERR! Status from the installers:
---------- Visual Studio Build Tools ----------
npm ERR! Still waiting for installer log file...
npm ERR! ------------------- Python --------------------
npm ERR! Successfully installed Python 2.7
npm ERR! ^C

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\80455\AppData\Local\npm-cache\_logs\2022-05-25T13_43_14_454Z-debug-0.log

到这里,我们再次输入 npm i
试图安装项目依赖,却发现还是有错误

PS D:\pratDemo\node_demo\node_dll> npm i
npm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile
npm ERR! code 1
npm ERR! path D:\pratDemo\node_demo\node_dll\node_modules\ffi-napi
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node-gyp-build
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@8.4.1

提示4: 从上一句npm ERR! gyp info using node-gyp@8.4.1
我们知道还需要安装node-gyp
然后:我们通过上文搜到的这篇文章:https://www.jianshu.com/p/7148b27eb61e
也提到我们需要安装node-gyp
所以我们输入 npm install -g node-gyp

PS D:\pratDemo\node_demo\node_dll> npm install -g node-gyp

added 94 packages, and audited 95 packages in 12s

5 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

到这里,没有报错信息,所有环境都安装好了,
最后我们输入 npm i
安装 package.json里面准备好的 ffi-napi

PS D:\pratDemo\node_demo\node_dll> npm i
npm WARN old lockfile
npm WARN old lockfile The package-lock.json file was created with an old version of npm,
npm WARN old lockfile so supplemental metadata must be fetched from the registry.
npm WARN old lockfile
npm WARN old lockfile This is a one-time fix-up, please be patient...
npm WARN old lockfile

added 1 package, removed 25 packages, changed 9 packages, and audited 11 packages in 29s

found 0 vulnerabilities

这里提示安装项目的依赖成功!

最后我们运行项目,成功:

PS D:\pratDemo\node_demo\node_dll> node .\ffi.js
结果: 9
PS D:\pratDemo\node_demo\node_dll>
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
macOS 安装 node-gyp 并遇到错误的情况通常是由于几个常见原因造成的: ### 报错情况概述 当你尝试通过 `npm install -g node-gyp` 来全局安装 node-gyp 到你的 macOS 系统上时,可能会遇到各种错误信息。这可能是由多种因素引起的,包括但不限于依赖库缺失、环境配置问题或者是网络问题。 ### 解决方案步骤 #### 1. 更新 Node.jsnpm 首先检查并更新到最新的 Node.js 版本,因为某些错误可能在新版本中已被修复。你可以通过命令行运行以下脚本来升级: ```bash sudo npm install -g n n stable ``` #### 2. 重新安装 node-gyp 如果更新了 Node.js 还无法解决安装问题,可以尝试直接卸载当前的 Node.js 和重新安装: ```bash sudo rm -rf /usr/local/n sudo rm -rf ~/.npm-global npm uninstall -g node-gyp ``` 然后从官方仓库下载最新版的 Node.js: ```bash wget https://nodejs.org/dist/latest/node-vXX.XX.X.tar.gz tar xzvf node-vXX.XX.X.tar.gz cd node-vXX.XX.X/ ./configure make sudo make install ``` 最后,重新安装 node-gyp: ```bash npm install -g node-gyp ``` #### 3. 检查系统权限和依赖项 有时候安装失败可能是由于缺少特定的编译工具,如 GCC(GNU Compiler Collection)。确保已安装最新版本的 GCC,并尝试再次安装 node-gyp: ```bash brew update brew upgrade gcc brew install pkg-config libffi ``` 或者对于非 Homebrew 用户,直接从网站下载并安装 GCC。 #### 4. 清理 Node 缓存 清理 npm 的缓存文件有时也能帮助解决问题: ```bash npm cache clean --force ``` #### 相关问题: 1. **如何检查 Node.jsnpm 是否已经正确安装并且版本是最新的?** - 使用 `node -v` 查看 Node.js 版本。 - 使用 `npm -v` 查看 npm 版本。 2. **在 Windows 上安装 node-gyp 遇到的问题有哪些不同点?** - Windows 下安装可能需要额外的依赖包,如 MSYS2 和 MinGW-w64 或者 Cygwin 环境,以便能够提供 C++ 编译所需的功能。 3. **在安装过程中遇到了错误消息“Missing SDK”、“Not a directory”,该如何处理?** - 这些错误通常意味着缺少必要的操作系统开发工具集。对于 macOS,确保你有足够的权限访问系统的开发者工具;对于其他平台,则可能需要安装对应的编译器套件和相关 SDK。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值