You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

安装opencv4nodejs失败求助:Node.js项目集成OpenCV遇问题

Node.js集成OpenCV时安装opencv4nodejs失败的解决方法

问题概述

尝试创建集成OpenCV的Node.js项目,安装opencv4nodejs及@u4/opencv4nodejs均失败,已尝试安装Chocolatey但无效,执行过以下命令:

  • npm install --save opencv4nodejs --build-from-source
  • npm install --save @u4/opencv4nodejs --force

错误信息

执行npm install --save @u4/opencv4nodejs --force时的错误输出:

PS D:\learnnode> npm install --save @u4/opencv4nodejs --force
npm warn using --force Recommended protections disabled.
npm warn cleanup Failed to remove some directories [
npm warn cleanup   [
npm warn cleanup     '\\?\\D:\\learnnode\\node_modules\\@u4\\opencv-build',
npm warn cleanup     [Error: EPERM: operation not permitted, rmdir 'D:\learnnode\node_modules\@u4\opencv-build\script\deps\jsr.io'] {
npm warn cleanup       errno: -4048,
npm warn cleanup       code: 'EPERM',
npm warn cleanup       syscall: 'rmdir',
npm warn cleanup       path: 'D:\\learnnode\\node_modules\\@u4\\opencv-build\\script\\deps\\jsr.io'
npm warn cleanup     }
npm warn cleanup   ]
npm warn cleanup ]
npm error code 1
npm error path D:\learnnode\node_modules\@u4\opencv4nodejs
npm error command failed
npm error command C:\Windows\system32\cmd.exe /d /s /c node-gyp rebuild
npm error gyp info it worked if it ends with ok
npm error gyp info using node-gyp@10.2.0
npm error gyp info using node@22.9.0 | win32 | x64
npm error gyp info find Python using Python version 3.9.0 found at "C:\Users\ideal\AppData\Local\Programs\Python\Python39\python.exe"
npm error gyp info find VS using VS2022 (17.11.35312.102) found at:
npm error gyp info find VS "C:\Program Files\Microsoft Visual Studio\2022\Community"
npm error gyp info find VS run with --verbose for detailed information
npm error gyp info spawn C:\Users\ideal\AppData\Local\Programs\Python\Python39\python.exe
npm error gyp info spawn args [
npm error gyp info spawn args 'D:\\learnnode\\node_modules\\node-gyp\\gyp\\gyp_main.py',
npm error gyp info spawn args 'binding.gyp',
npm error gyp info spawn args '-f',
npm error gyp info spawn args 'msvs',
npm error gyp info spawn args '-I',
npm error gyp info spawn args 'D:\\learnnode\\node_modules\\@u4\\opencv4nodejs\\build\\config.gypi',
npm error gyp info spawn args '-I',
npm error gyp info spawn args 'D:\\learnnode\\node_modules\\node-gyp\\addon.gypi',
npm error gyp info spawn args '-I',
npm error gyp info spawn args 'C:\\Users\\ideal\\AppData\\Local\\node-gyp\\Cache\\22.9.0\\include\\node\\common.gypi',
npm error gyp info spawn args '-Dlibrary=shared_library',
npm error gyp info spawn args '-Dvisibility=default',
npm error gyp info spawn args '-Dnode_root_dir=C:\\Users\\ideal\\AppData\\Local\\node-gyp\\Cache\\22.9.0',
npm error gyp info spawn args '-Dnode_gyp_dir=D:\\learnnode\\node_modules\\node-gyp',
npm error gyp info spawn args '-Dnode_lib_file=C:\\\\Users\\\\ideal\\\\AppData\\\\Local\\\\node-gyp\\\\Cache\\\\22.9.0\\\\<(target_arch)\\\\node.lib',
npm error gyp info spawn args '-Dmodule_root_dir=D:\\learnnode\\node_modules\\@u4\\opencv4nodejs',
npm error gyp info spawn args '-Dnode_engine=v8',
npm error gyp info spawn args '--depth=.',
npm error gyp info spawn args '--no-parallel',
npm error gyp info spawn args '--generator-output',
npm error gyp info spawn args 'D:\\learnnode\\node_modules\\@u4\\opencv4nodejs\\build',
npm error gyp info spawn args '-Goutput_dir=.'
npm error gyp info spawn args ]
npm error gyp: Call to 'node ./bin/install.js OPENCV4NODEJS_DEFINES' returned exit status 1 while in binding.gyp. while trying to load binding.gyp
npm error gyp ERR! configure error
npm error gyp ERR! stack Error: `gyp` failed with exit code: 1
npm error gyp ERR! stack at ChildProcess.<anonymous> (D:\learnnode\node_modules\node-gyp\lib\configure.js:317:18)
npm error gyp ERR! stack at ChildProcess.emit (node:events:519:28)
npm error gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:293:12)
npm error gyp ERR! System Windows_NT 10.0.22631
npm error gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "D:\\learnnode\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm error gyp ERR! cwd D:\learnnode\node_modules\@u4\opencv4nodejs
npm error gyp ERR! node -v v22.9.0
npm error gyp ERR! node-gyp -v v10.2.0
npm error gyp ERR! not ok
npm error A complete log of this run can be found in: C:\Users\ideal\AppData\Local\npm-cache\_logs\2024-09-23T19_34_06_698Z-debug-0.log

解决步骤

1. 清理残留文件,解决权限问题

  • 关闭所有占用项目目录的程序(如VS Code、资源管理器)
  • 手动删除node_modules文件夹和package-lock.json文件
  • 以管理员身份打开PowerShell,执行:
    npm cache clean --force
    

2. 降低Node.js版本

Node.js 22属于较新版本,可能与@u4/opencv4nodejs兼容性不佳,建议降级到LTS版本(如18.x或20.x):

  • 使用nvm(Node版本管理器)安装指定版本:
    nvm install 20.17.0
    nvm use 20.17.0
    
  • 未安装nvm的话,可直接从Node.js官网下载对应版本安装包覆盖安装。

3. 手动安装OpenCV并指定路径

避免自动安装时的依赖问题,手动下载配置OpenCV:

  • 从OpenCV官网下载4.x系列Windows版本安装包,解压到本地路径(如C:\opencv)
  • 设置系统环境变量:
    • 添加OPENCV_INCLUDE_DIR:C:\opencv\build\include
    • 添加OPENCV_LIB_DIR:C:\opencv\build\x64\vc16\lib
    • 将C:\opencv\build\x64\vc16\bin添加到Path环境变量
  • 重启终端,执行安装命令时指定OpenCV路径:
    npm install --save @u4/opencv4nodejs --opencv4nodejs_opencv_binary=4.5.5 --opencv4nodejs_opencv_lib_dir=C:\opencv\build\x64\vc16\lib --opencv4nodejs_opencv_include_dir=C:\opencv\build\include
    

4. 确保编译工具正常

  • 确认Visual Studio 2022已安装**桌面开发C++**组件
  • 确认Python 3.9已添加到系统Path环境变量,版本需符合node-gyp要求(3.6-3.11均可)

内容的提问来源于stack exchange,提问作者Wharton Aldrick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 05:23:09