Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Cannot find module 'XXX/node_modules/.pnpm/@[email protected]/node_modules/@u4/opencv4nodejs/build/Release/opencv4nodejs' #122

Open
junehunter opened this issue Jan 12, 2024 · 6 comments

Comments

@junehunter
Copy link

junehunter commented Jan 12, 2024

/XXX/node_modules/.pnpm/@u4[email protected]/node_modules/@u4/opencv4nodejs/lib/cvloader.js:62
throw err;
^

Error: Cannot find module '/XXX/node_modules/.pnpm/@u4[email protected]/node_modules/@u4/opencv4nodejs/build/Release/opencv4nodejs'
Require stack:

  • /XXX/node_modules/.pnpm/@u4[email protected]/node_modules/@u4/opencv4nodejs/lib/cvloader.js
  • /XXX/node_modules/.pnpm/@u4[email protected]/node_modules/@u4/opencv4nodejs/lib/opencv4nodejs.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1026:15)
    at Function.Module._resolveFilename (/home/develop/nodejs/tensorflow-node/node_modules/.pnpm/[email protected]/node_modules/module-alias/index.js:49:29)
    at Function.Module._load (node:internal/modules/cjs/loader:871:27)
    at Module.require (node:internal/modules/cjs/loader:1098:19)
    at require (node:internal/modules/cjs/helpers:108:18)
    at getOpenCV (/home/develop/nodejs/tensorflow-node/node_modules/.pnpm/@u4[email protected]/node_modules/@u4/opencv4nodejs/lib/cvloader.js:54:23)
    at loadOpenCV (/home/develop/nodejs/tensorflow-node/node_modules/.pnpm/@u4[email protected]/node_modules/@u4/opencv4nodejs/lib/opencv4nodejs.js:6:48)
    at Object. (/home/develop/nodejs/tensorflow-node/node_modules/.pnpm/@u4[email protected]/node_modules/@u4/opencv4nodejs/lib/opencv4nodejs.js:21:12)
    at Module._compile (node:internal/modules/cjs/loader:1196:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1250:10) {
    code: 'MODULE_NOT_FOUND',
@junehunter junehunter changed the title Error: Cannot find module '/home/develop/nodejs/tensorflow-node/node_modules/.pnpm/@[email protected]/node_modules/@u4/opencv4nodejs/build/Release/opencv4nodejs' Error: Cannot find module 'XXX/node_modules/.pnpm/@[email protected]/node_modules/@u4/opencv4nodejs/build/Release/opencv4nodejs' Jan 15, 2024
@sollosollo4
Copy link

I have same issue after packaged application and install it on another PC

@AbaoFromCUG
Copy link

@junehunter Need to build build/Release/opencv4nodejs.node first via build-opencv command, read more in README.md

@sollosollo4 do you copy/bundle opencv4nodejs.node in your application?

@sollosollo4
Copy link

@AbaoFromCUG what do u mean? I gues yes, but how I can check this. Im not copy, but this going up in app.asar.unpacked when building
But in app.asar, I have links not to the folder inside builds, but to the folder in the full path C:/ on my PC. I think this is a mistake, but I don’t know how to tell the builder to specify asar relative paths instead of relative build paths.

@sollosollo4
Copy link

if I have tme, I'll check what happends if I require library by next code

const libraryPath = path.join(__dirname, 'app.asar', 'absolute path to opencv4nodejs.node library inside app_asar folder');

const yourLibrary = require(libraryPath);

@junehunter
Copy link
Author

@junehunter Need to build build/Release/opencv4nodejs.node first via build-opencv command, read more in README.md

@sollosollo4 do you copy/bundle opencv4nodejs.node in your application?

The Windows environment builds successfully, but Linux does not build the opencv4nodejs binary correctly.

@AbaoFromCUG
Copy link

@AbaoFromCUG what do u mean? I gues yes, but how I can check this. Im not copy, but this going up in app.asar.unpacked when building But in app.asar, I have links not to the folder inside builds, but to the folder in the full path C:/ on my PC. I think this is a mistake, but I don’t know how to tell the builder to specify asar relative paths instead of relative build paths.

This is the logic that should be implemented (but currently it is not)

Normally, Bundle (like webpack/vite) will copy opencv4nodejs.node to your outputPath(dist usually), like node-loader of webpack

The package maintainer should write code like require(`../bin/${process.platform}/{process.arch}/opencv4nodejs.node`) to load addon-bindings. The package folder like

- opencv4nodejs
----| bin
--------| win32
------------| x64
----------------|opencv4nodejs.node
----------------|opencv.dll
----| lib
--------| index.js

When we use the package in package.json and webpack+node-loader, the output Of our project should be

- myProject
----| dist
--------| bin
------------| win32
----------------| x64
--------------------|opencv4nodejs.node
--------------------|opencv.dll
--------| index.js

and the code in index.js will become

__webpack_require__("./node_modules/.pnpm/......\.node$")(`./${process.platform}/${process.arch}/opencv4nodejs.node`)

Just like onnxruntime-node did

https://github.com/microsoft/onnxruntime/blob/966fa74597affcaffe35968b5bae0bab5d782f7c/js/node/lib/binding.ts#L45-L52

What we can do?

modify the relevant source code, but it is hard…… You need hack the cvloader.js , these code may be useful, but not enough, you need copy dll/node file to bin/napi-v3/win32/64/ too……This is too troublesome

const cvloaderPath = "node_modules/@u4/opencv4nodejs/lib/cvloader.js"
// need copy node/binary to this folder……
const binaryDestPath = "node_modules/@u4/opencv4nodejs/bin/napi-v3"

const main = async()=> {
    const buf = await fs.readFile(cvloaderPath)
    let content = buf.toString()

    content = content.replace(/(function getOpenCV\(opt\))[.\s\S]*?(\s*\/\/ resolve haarcascade files)/, "$1{\n    const opencvBuild=require(`../bin/napi-v3/${process.platform}/${process.arch}/opencv4nodejs.node`)\n$2");

    await fs.writeFile(cvloaderPath, content)
    console.log("hack cvloader.js success")
}
main()

The simple step to copy dll/node to the side of app.asar?(I guess)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants