opencv4nodejs: Can't install opencv4nodejs

  1. I tried to install into RasberryPi.
  2. I installed CMake already
  3. I set
  • export OPENCV4NODEJS_DISABLE_AUTOBUILD=0
  • export OPENCV4NODEJS_AUTOBUILD_OPENCV_VERSION=4.1.0
  1. I tried to install opencv4nodejs via npm i opencv4nodejs
  2. I got the below error.

Error Message

$ npm i opencv4nodejs

> opencv4nodejs@5.6.0 install /home/pi/opencv-virtualuvc/node_modules/opencv4nodejs
> node ./install/install.js

info the following opencv4nodejs environment variables are set in the package.json: 
info autoBuildFlags: -DOPENCV_GENERATE_PKGCONFIG=ON -DOPENCV_PC_FILE_NAME=opencv.pc 
info autoBuildOpencvVersion: 4.1.0 
info install OPENCV_LIB_DIR is not set, looking for default lib dir
info install using lib dir: /usr/local/lib
/home/pi/opencv-virtualuvc/node_modules/opencv4nodejs/install/install.js:45
  throw new Error('no OpenCV libraries found in lib dir: ' + libDir)
  ^

Error: no OpenCV libraries found in lib dir: /usr/local/lib
    at Object.<anonymous> (/home/pi/opencv-virtualuvc/node_modules/opencv4nodejs/install/install.js:45:9)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! opencv4nodejs@5.6.0 install: `node ./install/install.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the opencv4nodejs@5.6.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/pi/.npm/_logs/2020-06-19T22_35_51_386Z-debug.log

Auto Build - Full Error Log

I’d like Auto Build. But I don’t know where Auto Build Error Log is.

OpenCV version (example 3.4.1): ?.?.?

With OpenCV-contrib? (extra modules): ?

OS: Raspbian GNU/Linux 10 (buster)

$ lsb_release -a
No LSB modules are available.
Distributor ID: Raspbian
Description:    Raspbian GNU/Linux 10 (buster)
Release:        10
Codename:       buster

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 41 (2 by maintainers)

Most upvoted comments

My steps for Mac using Intel chip

  1. brew install opencv
  2. export OPENCV4NODEJS_DISABLE_AUTOBUILD=1
  3. npm i -g opencv4nodejs

Worked fine, using node v16.13.1

I have done the manual installation steps and was able to make it work: https://github.com/justadudewhohacks/opencv4nodejs#installing-opencv-manually

  1. Install Chocolatey
  2. Open cmd and set his env variable set OPENCV4NODEJS_DISABLE_AUTOBUILD=1
  3. Install OpenCV with this command: choco install OpenCV -y -version 4.1.0

By default it installed OpenCV on my C:\tools folder, so change the folders below according to your needs

  1. Set more env vars: set OPENCV_INCLUDE_DIR=C:\tools\opencv\build\include set OPENCV_LIB_DIR=C:\tools\opencv\build\x64\vc15\lib set OPENCV_BIN_DIR=C:\tools\opencv\build\x64\vc15\bin set PATH=%PATH%;%OPENCV_BIN_DIR%; set OPENCV4NODEJS_AUTOBUILD_OPENCV_VERSION=4.1.0
  2. Run npm i -g --save opencv4nodejs

With this I was able to get a successful installation of opencv4nodejs.

I did this installation to be able to use Appium findByImage feature, if you are trying to achieve the same then you might face more issues as I did, I was also able to get those solved but I did so much digging that I can’t remember every step I’ve done, but I found the problem was that node was having an issue finding the location of opencv in modules.

If you wanna do a quick check if you opencv is properly installed, create a project with the following content:

root/node_modules/
root/app.js

On app.js enter the following:

let opencv = require('opencv4nodejs');
console.log("success")

Then try to run this using node: node app.js, if it works then you just need to do the npm link between appium and opencv4nodejs.

macOS M1

brew install opencv export OPENCV_LIB_DIR=/opt/homebrew/opt/opencv@4/lib && export OPENCV4NODEJS_DISABLE_AUTOBUILD=1 npm i opencv4nodejs --save

throw new Error('no OpenCV libraries found in lib dir: ' + libDir) indicate that it is looking for the libDir-folder which should be set when not using autobuild.

Does the error persist if you unset OPENCV4NODEJS_DISABLE_AUTOBUILD? I experienced that the mere presence of the env variable was interpreted as “truthy” no matter the value.

Edit: Which I have opened a PR to solve - https://github.com/justadudewhohacks/npm-opencv-build/pull/42

specify environment variables or add them to the package.json

"dependencies": {
  "opencv4nodejs": "^5.6.0"
},
"opencv4nodejs": {
  "disableAutoBuild": 1,
  "opencvIncludeDir": "C:\\tools\\opencv\\build\\include",
  "opencvLibDir": "C:\\tools\\opencv\\build\\x64\\vc14\\lib",
  "opencvBinDir": "C:\\tools\\opencv\\build\\x64\\vc14\\bin"
},

For Mac OS: Add following settings to package.json. then run npm i opencv4nodejs

"opencv4nodejs": {
    "disableAutoBuild": 1,
    "opencvIncludeDir": "/opt/homebrew/opt/opencv@4/include",
    "opencvLibDir": "/opt/homebrew/opt/opencv@4/lib",
    "opencvBinDir": "/opt/homebrew/opt/opencv@4/bin"
  },

Must linux version install opencv on /usr/lib folder instead of /usr/local/lib. And the opencv-build dont´t autodetect folder from cmake files installed by opencv.

Maybe a quick solution just inform the environment variable OPENCV_LIB_DIR according to the opencv4nodejs documentation.

For me, this solution solved the problem.

export OPENCV_LIB_DIR=/usr/lib && export OPENCV4NODEJS_DISABLE_AUTOBUILD=1 && npm install opencv4nodejs

Assuming you’re not using autobuild, the issue is you aren’t telling npm where your OpenCV libraries are located!

I’m using Ubuntu 22. Here were my steps:

  1. Install opencv using package manage: apt install libopencv-dev python3-opencv

  2. Find location of where apt has installed the libraries using dpkg -L libopencv-dev (in my case, libraries were in /usr/lib/x86_64-linux-gnu )

  3. Set autobuild off and set location of libraries: export OPENCV4NODEJS_DISABLE_AUTOBUILD=1 export OPENCV_LIB_DIR=/usr/lib/x86_64-linux-gnu

  4. Install node module npm install opencv4nodejs

Good luck!

As my researching, there are no dylib files in node_modules/opencv-build/opencv/build/lib folder on my MacOS. That is the reason why we get no OpenCV libraries found in lib dir error message. When I use brew to install opencv, dylib files exist.

unset OPENCV4NODEJS_DISABLE_AUTOBUILD

This seemed to resolve this particular issue for me. Thank you 👍

Assuming you’re not using autobuild, the issue is you aren’t telling npm where your OpenCV libraries are located!

I’m using Ubuntu 22. Here were my steps:

  1. Install opencv using package manage: apt install libopencv-dev python3-opencv
  2. Find location of where apt has installed the libraries using dpkg -L libopencv-dev (in my case, libraries were in /usr/lib/x86_64-linux-gnu )
  3. Set autobuild off and set location of libraries: export OPENCV4NODEJS_DISABLE_AUTOBUILD=1 export OPENCV_LIB_DIR=/usr/lib/x86_64-linux-gnu
  4. Install node module npm install opencv4nodejs

Good luck!

its work

macOS M1

brew install opencv export OPENCV_LIB_DIR=/opt/homebrew/opt/opencv@4/lib && export OPENCV4NODEJS_DISABLE_AUTOBUILD=1 npm i opencv4nodejs --save

change npm i opencv4nodejs --save to npm i -g opencv4nodejs and appium-doctor all fun

macOS M1

brew install opencv export OPENCV_LIB_DIR=/opt/homebrew/opt/opencv@4/lib && export OPENCV4NODEJS_DISABLE_AUTOBUILD=1 npm i opencv4nodejs --save

brew install opencv

export OPENCV_LIB_DIR=/opt/homebrew/opt/opencv@4/lib export OPENCV4NODEJS_DISABLE_AUTOBUILD=1 export OPENCV_BIN_DIR=/opt/homebrew/opt/opencv@4/bin export OPENCV_INCLUDE_DIR=/opt/homebrew/opt/opencv@4/include/opencv4

npm install --save @u4/opencv4nodejs

node -e “console.log(require(‘@u4/opencv4nodejs’))”

I have done the manual installation steps and was able to make it work: https://github.com/justadudewhohacks/opencv4nodejs#installing-opencv-manually

  1. Install Chocolatey
  2. Open cmd and set his env variable set OPENCV4NODEJS_DISABLE_AUTOBUILD=1
  3. Install OpenCV with this command: choco install OpenCV -y -version 4.1.0

By default it installed OpenCV on my C:\tools folder, so change the folders below according to your needs

  1. Set more env vars: set OPENCV_INCLUDE_DIR=C:\tools\opencv\build\include set OPENCV_LIB_DIR=C:\tools\opencv\build\x64\vc15\lib set OPENCV_BIN_DIR=C:\tools\opencv\build\x64\vc15\bin set PATH=%PATH%;%OPENCV_BIN_DIR%; set OPENCV4NODEJS_AUTOBUILD_OPENCV_VERSION=4.1.0
  2. Run npm i -g --save opencv4nodejs

With this I was able to get a successful installation of opencv4nodejs. I did this installation to be able to use Appium findByImage feature, if you are trying to achieve the same then you might face more issues as I did, I was also able to get those solved but I did so much digging that I can’t remember every step I’ve done, but I found the problem was that node was having an issue finding the location of opencv in modules. If you wanna do a quick check if you opencv is properly installed, create a project with the following content:

root/node_modules/
root/app.js

On app.js enter the following:

let opencv = require('opencv4nodejs');
console.log("success")

Then try to run this using node: node app.js, if it works then you just need to do the npm link between appium and opencv4nodejs.

Everyone who is coming to this at a later date this is the only working solution I found

And guess what… It doesn’t work for me

My steps for Mac using Intel chip

  1. brew install opencv
  2. export OPENCV4NODEJS_DISABLE_AUTOBUILD=1
  3. npm i -g opencv4nodejs

Worked fine, using node v16.13.1

This should be the solution even in 2023.

My steps for Mac using Intel chip

  1. brew install opencv
  2. export OPENCV4NODEJS_DISABLE_AUTOBUILD=1
  3. npm i -g opencv4nodejs

Worked fine, using node v16.13.1

worked fine for me on mac M1, thanks!

doesn’t work on my m1 monterey too