M1安装opencv4nodejs教程

正常来说node-gyp版本应该和你的nodejs匹配,你这个版本貌似有点太低了。。。
卸载掉重新安装一下再试试看吧
我本地装的是9.1.0版本

ERR! install node-gyp failed and return Error Command failed: node-gyp build --jobs max --release
ERR! install gyp info it worked if it ends with ok
ERR! install gyp info using node-gyp@9.1.0
ERR! install gyp info using node@16.16.0 | darwin | arm64
ERR! install gyp ERR! build error
ERR! install gyp ERR! stack Error: You must run node-gyp configure first!
ERR! install gyp ERR! stack at ReadFileContext. (/usr/local/lib/node_modules/node-gyp/lib/build.js:43:20)
ERR! install gyp ERR! stack at ReadFileContext.callback (/usr/local/lib/node_modules/node-gyp/node_modules/graceful-fs/graceful-fs.js:123:16)
ERR! install gyp ERR! stack at FSReqCallback.readFileAfterOpen [as oncomplete] (node:fs:314:13)

我用了9.1.0也不行,然后看提示执行这个命令node-gyp configure 报这个错

gyp: binding.gyp not found (cwd: /Users/liuhong) while trying to load binding.gyp
gyp ERR! configure error 
gyp ERR! stack Error: `gyp` failed with exit code: 1
gyp ERR! stack     at ChildProcess.onCpExit (/usr/local/lib/node_modules/node-gyp/lib/configure.js:284:16)
gyp ERR! stack     at ChildProcess.emit (node:events:527:28)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12)
gyp ERR! System Darwin 20.4.0
gyp ERR! command "/usr/local/bin/node" "/usr/local/bin/node-gyp" "configure"
gyp ERR! cwd /Users/liuhong
gyp ERR! node -v v16.16.0
gyp ERR! node-gyp -v v9.1.0
gyp ERR! not ok 

完整日志发一下吧 放到txt文件里发出来,只有这一段不够详细

error.txt (2.1 KB)
error2.txt (12.3 KB)麻烦老师帮忙看看

liuhong@liuhonghongdeMacBook-Pro node_modules % brew info opencv
opencv: stable 4.5.2 (bottled)
Open source computer vision library
https://opencv.org/
/opt/homebrew/Cellar/opencv/4.5.2_4 (835 files, 120.6MB) *
  Poured from bottle on 2022-08-03 at 10:32:25
/opt/homebrew/Cellar/opencv/4.5.2_4.bak (835 files, 120.6MB)
  Poured from bottle on 2022-08-01 at 20:59:22
From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/opencv.rb
License: Apache-2.0
==> Dependencies
Build: cmake ✔, pkg-config ✔
Required: ceres-solver ✔, eigen ✔, ffmpeg ✔, glog ✔, harfbuzz ✔, jpeg ✔, libpng ✔, libtiff ✔, numpy ✔, openblas ✔, openexr ✔, protobuf ✔, python@3.9 ✔, tbb ✔, vtk ✔, webp ✔
==> Analytics
install: 15,564 (30 days), 40,608 (90 days), 181,668 (365 days)
install-on-request: 15,018 (30 days), 39,246 (90 days), 174,922 (365 days)
build-error: 54 (30 days)

这会不会是个重要信息build-error: 54 (30 days)

image
是导入问题,发完整的日志看看不要节选。。。
从你执行build-opencv这一行命令开始 一直到结束

error2.txt (16.3 KB)

  • 修改一下你的环境变量
export OPENCV_INCLUDE_DIR=/opt/homebrew/Cellar/opencv/4.5.2_4/include/opencv4
  • 然后source重新加载一下环境变量
  • brew安装的opencv4目录结构跟之前不太一样 include目录下不是直接放的opencv2而是多了一层opencv4目录
  • 之后再重新运行一下试试看

error2.txt (21.5 KB)
刚才重新跑一次是这个保存,和改了你说的这个环境变量前后一个报错

改完设置重新卸载安装一下node-gyp试试呢?
我之前也是改完一直加载不到对应的文件,即便文件目录没错,尝试重装了一次之后突然好了

重装node-gyp还是报这个错

/opt/homebrew/lib/node_modules/@u4/opencv4nodejs
到这个目录下执行一下node-gyp configure 再试试?

node-gyp configure 执行没问题,但是再试还是报那个错,看起来是执行node-gyp build --jobs max --release 这个报的错

直接原因就是导入头文件的时候没有找到opencv2/core.hpp

这个openv2在哪啊,找不到,是不是要重装什么才会有

这个目录下没有么?这个头文件是opencv4自带的

换成你的版本4.5.5_3 又报这个错
error2.txt (4.3 KB)

opencv4里面也有openv2

我给出了两种安装方法,你先确定好用哪一种
如果用的是自己安装opencv的方式,版本要写自己安装的版本,不要跟着我写的版本来写呀。。

另外还发现了一个人的结局方案 也挺简单的 准备步骤跟我这个差不多,最后安装的库是装的另外一个。这个库是否后续还会维护不太清楚,只建议临时使用
https://github.com/justadudewhohacks/opencv4nodejs/issues/538#issuecomment-1015480008

我本地执行编译报错,这个环境有点难弄啊
mac系统


感觉像是c++环境问题,求指导