Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

The specified module could not be found. (.node) #1135

Closed
skaneprime opened this issue Apr 15, 2021 · 28 comments
Closed

The specified module could not be found. (.node) #1135

skaneprime opened this issue Apr 15, 2021 · 28 comments

Comments

@skaneprime
Copy link

While building. Pkg throwed me some warnings about native modules
image

And I did as it told me to do. I copied it and moved to bin directory. So folder looks like this
image

However, when I launch executable, it throws me this error
Error: The specified module could not be found. \\?\D:\WORK\bothub\discord-base-bot\canvas.node

image

What's the issue?

in package.json
"pkg": { "scripts": [ "./dist/core/**/*.js", "./dist/services/**/*.js", "./dist/plugins/**/*.js" ], "targets": [ "node14-win-x64" ] }

@skaneprime
Copy link
Author

I also tried to include native modules in "assets" but it didn't change anything

@robertsLando
Copy link
Contributor

@skaneprime are you using latest version?

@skaneprime
Copy link
Author

skaneprime commented Apr 15, 2021

@robertsLando
I was using 4.4.9. Updated to 4.5.1 But now it launches half of the code and just stops
with node
image

after building with pkg
image

@skaneprime
Copy link
Author

Okay now here's detailed one. (Sorry, I'm kinda rushy guy)

When I'm not including (.node) files as assets it works but throws such errors
image

when I include them the same mistakes occur

image

@robertsLando
Copy link
Contributor

Try to provide a relative path to the nodes instead of using a full path, start from ./node_modules/..

@skaneprime
Copy link
Author

Try to provide a relative path to the nodes instead of using a full path, start from ./node_modules/..

Same error
image

@skaneprime
Copy link
Author

I tried to play with paths. It didn't change anything. Even copying native modules to another folder and adding them as asset

@robertsLando
Copy link
Contributor

@erossignon Any clue?

@skaneprime
Copy link
Author

I even tried to include the whole module it self as an asset but the result is same "node_modules/canvas/**/*"

@robertsLando
Copy link
Contributor

@leerob @jesec @erossignon Seems that pkg isn't able to correctly bind .node files. Tests are passing and there are tests that are covering this, any clue what could be wrong? Maybe the PR change that allow to integrate .node binaries in the exe broke this someway?

@skaneprime
Copy link
Author

@erossignon Hi! Do you have any ideas?

@erossignon
Copy link
Contributor

erossignon commented Apr 22, 2021

So I've createc a simple nodejs project that includes canvas

package.json

{
  "name": "pkg_issue_1335",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "bin": "./index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "pkg42": "npx pkg -t host package.json"
  },
  "pkg": {
    "scripts": "index.js",
    "assets": "node_modules/canvas/build/Release/*",
    "outputPath": "dist"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "canvas": "^2.7.0"
  }
}
// ìndex.js
const canvas = require("canvas");
console.log("42");

It seems that canvas.node is no more a statically linked dll and requires a lot of dependent dll to be loaded:

ls ./node_modules/canvas/build/Release/
canvas.exp       libbrotlicommon.dll     libexpat-1.dll       libgdk_pixbuf-2.0-0.dll  libgraphite2.dll   libpango-1.0-0.dll       libpng16-16.dll      obj
canvas.ilk       libbrotlidec.dll        libffi-7.dll         libgif-7.dll             libharfbuzz-0.dll  libpangocairo-1.0-0.dll  librsvg-2-2.dll      zlib1.dll
canvas.lib       libbz2-1.dll            libfontconfig-1.dll  libgio-2.0-0.dll         libiconv-2.dll     libpangoft2-1.0-0.dll    libstdc++-6.dll
canvas.node      libcairo-2.dll          libfreetype-6.dll    libglib-2.0-0.dll        libintl-8.dll      libpangowin32-1.0-0.dll  libthai-0.dll
canvas.node.bak  libcairo-gobject-2.dll  libfribidi-0.dll     libgmodule-2.0-0.dll     libjpeg-8.dll      libpcre-1.dll            libwinpthread-1.dll
canvas.pdb       libdatrie-1.dll         libgcc_s_seh-1.dll   libgobject-2.0-0.dll     liblzma-5.dll      libpixman-1-0.dll        libxml2-2.dll

I run

npx pkg package.json

to generate the binary,
then run the binary itself

$./pkg_issue_1335-linux

and got:

pkg/prelude/bootstrap.js:1359
      throw error;
      ^

Error: /tmp/a06a6d1818170db6aa30d25ee7d26b2ea12cffd0b4dcf34aa3bde8d57a2406f3_canvas.node: invalid ELF header
    at tryImporting (pkg/prelude/bootstrap.js:1736:37)
    at process.dlopen (pkg/prelude/bootstrap.js:1762:5)
    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1248:18)
    at Module.load (internal/modules/cjs/loader.js:1047:32)
    at Function.Module._load (internal/modules/cjs/loader.js:935:14)
    at Module.require (internal/modules/cjs/loader.js:1087:19)
    at Module.require (pkg/prelude/bootstrap.js:1338:31)
    at require (internal/modules/cjs/helpers.js:73:18)
    at Object.<anonymous> (/snapshot/pkg_issue_1335/node_modules/canvas/lib/bindings.js:3:18)
    at Module._compile (pkg/prelude/bootstrap.js:1433:22)

pkg seems to copy the canvas.node to a temporary folder before loading it .

pkg has currently no way to detect that external dll are need, the system dll loader is not aware of the pkg virtual file system.

A possibility would be to extract all files that are inside the folder in the same temporary folder as "canvas.node" binary is at runtime so that dll loader works as expected.

@robertsLando
Copy link
Contributor

I think that adding ./node_modules/canvas/build/Release/* in assets could fix the problem

@erossignon
Copy link
Contributor

erossignon commented Apr 22, 2021

In fact, my mistake, I was mixing with a previous attempt on Windows, hence the invalid ELF header error.

I should have run dist/pkg_issue_1335-linux

and it works ! (on linux)

Running .\dist\pkg_issue_1335-win.exe on windows exhibit this issue:

Error: C:\Users\xxxxx\AppData\Local\Temp/671674c55d8bd6d5211863f8185778ec98f1063105350bf4a9de8639814155e9_canvas.node is not a valid Win32 application.
C:\Users\xxxxxx\AppData\Local\Temp/671674c55d8bd6d5211863f8185778ec98f1063105350bf4a9de8639814155e9_canvas.node
    at tryImporting (pkg/prelude/bootstrap.js:1736:37)
    at process.dlopen (pkg/prelude/bootstrap.js:1762:5)

@erossignon
Copy link
Contributor

I think that adding ./node_modules/canvas/build/Release/* in assets could fix the problem

That might not be enough as those assets will have to be copied also where the .node file is extracted.

@robertsLando
Copy link
Contributor

Ok so the only way would be to manually copy those files un root directory of the executable

@erossignon
Copy link
Contributor

The issue takes place in bootstrap here

const unknownModuleErrorRegex = /([^:]+): cannot open shared object file: No such file or directory/;

   const unknownModuleErrorRegex = /([^:]+): cannot open shared object file: No such file or directory/;
    const tryImporting = function tryImporting(previousErrorMessage) {

the dlopen on the canvas.node doesn't not create a error message that matched the unknownModuleErrorRegex pattern.
and the tryImporting doesn't trigger .

The message generated by canvas doesn't contain the name of the missing dll. It defeats completely the assumption made by tryImporting`

erossignon added a commit to node-opcua/pkg that referenced this issue Apr 22, 2021
erossignon added a commit to node-opcua/pkg that referenced this issue Apr 22, 2021
erossignon added a commit to node-opcua/pkg that referenced this issue Apr 22, 2021
erossignon added a commit to node-opcua/pkg that referenced this issue Apr 22, 2021
erossignon added a commit to node-opcua/pkg that referenced this issue Apr 22, 2021
erossignon added a commit to node-opcua/pkg that referenced this issue Apr 22, 2021
erossignon added a commit to node-opcua/pkg that referenced this issue Apr 22, 2021
erossignon added a commit to node-opcua/pkg that referenced this issue Apr 22, 2021
erossignon added a commit to node-opcua/pkg that referenced this issue Apr 23, 2021
erossignon added a commit to node-opcua/pkg that referenced this issue Apr 23, 2021
erossignon added a commit to node-opcua/pkg that referenced this issue Apr 23, 2021
erossignon added a commit to node-opcua/pkg that referenced this issue Apr 23, 2021
@erossignon
Copy link
Contributor

@skaneprime can you confirm that issue is now resolved with pkg@5.1 ?

@erossignon erossignon reopened this May 2, 2021
@skaneprime
Copy link
Author

let me rebuild it then

@skaneprime can you confirm that issue is now resolved with pkg@5.1 ?

Yes! It works!!! Thank you very much!

@yangchen08
Copy link

yangchen08 commented Sep 22, 2021

@erossignon I still got this issue with pkg 5.3.2 on mac. No library found in tmp path.
Error: dlopen(/var/folders/z6/fsq4vw8x7sq2ysb9rdsq4xf80000gn/T/3e57d87ecc7dcca3b8c85e1103e9b717e9d94f0583cad9042a73ef0aafe00c8d/canvas.node, 1): Library not loaded: @loader_path/librsvg-2.2.dylib Referenced from: /var/folders/z6/fsq4vw8x7sq2ysb9rdsq4xf80000gn/T/3e57d87ecc7dcca3b8c85e1103e9b717e9d94f0583cad9042a73ef0aafe00c8d/canvas.node Reason: image not found

@sebastianrath
Copy link

sebastianrath commented Sep 28, 2021

@yangchen08 you can fix this by using Homebrew.

brew install librsvg

But note, this is only a local/temporary fix as the resulting executable expects the library to be located at /opt/homebrew/... which might not be the case on another machine.

But I am also looking for a solution how to tell pkg to embed these dependencies as well. Is this about a missing dependency resolver for node files? @erossignon On Windows it's clear as the *.dll files are already stored in the node_modules directory, but why does this work on Linux?

@prasheel888
Copy link

how to resolve this canvas issue??

@haroldiedema
Copy link

This is still an issue on Windows. Executables packaged with pkg simply cannot "find" .node files when running on Windows. Even debugging with fs.existsSync() with the same file as what is passed to require() says the file exists, yet we keep getting the same ERR_DLOPEN_FAILED message.

I've tried every version of pkg going back to 5.0. I've also tried every possible fix/workaround I could find from reported issues. (including moving .node files and its dependencies to the exec path, adding stuff to the "assets" list in package.json, referencing the node file statically instead of a dynamic import, etc. nothing works, its just a broken feature).

In my case, my .node addon relies on external dlls that should be present alongside the .node file (glfw.node in my case, which requires opengl libraries).

Everything works fine on Linux, its just Windows that fails. I have a feeling the "The specified module could not be found" error message is misleading and that it actually fails on loading the external DLL files.

@Lucky38i
Copy link

Lucky38i commented Jun 8, 2022

This is still an issue on Windows. Executables packaged with pkg simply cannot "find" .node files when running on Windows. Even debugging with fs.existsSync() with the same file as what is passed to require() says the file exists, yet we keep getting the same ERR_DLOPEN_FAILED message.

I've tried every version of pkg going back to 5.0. I've also tried every possible fix/workaround I could find from reported issues. (including moving .node files and its dependencies to the exec path, adding stuff to the "assets" list in package.json, referencing the node file statically instead of a dynamic import, etc. nothing works, its just a broken feature).

In my case, my .node addon relies on external dlls that should be present alongside the .node file (glfw.node in my case, which requires opengl libraries).

Everything works fine on Linux, its just Windows that fails. I have a feeling the "The specified module could not be found" error message is misleading and that it actually fails on loading the external DLL files.

I pretty much just distribute the release folder of .node libraries that can't be bundled with pkg, so far that's been skis-canvas and sharp

@haroldiedema
Copy link

So right after I posted my previous message, I ran Dependency Walker on the .node file and noticed some native Windows API's were missing. I just "guessted" a solution and went ahead and installed both .net runtime and vc redist (just google them) and it magically worked.

Just leaving this here for others that may get stuck on the ERR_DLOPEN_FAILED messages.

Links:

Note that your customers machines may need these too if they aren't installed.

@prasheel888
Copy link

So right after I posted my previous message, I ran Dependency Walker on the .node file and noticed some native Windows API's were missing. I just "guessted" a solution and went ahead and installed both .net runtime and vc redist (just google them) and it magically worked.

Just leaving this here for others that may get stuck on the ERR_DLOPEN_FAILED messages.

Links:

Note that your customers machines may need these too if they aren't installed.

Bro I'm still getting
pkg/prelude/bootstrap.js:1430
throw error;
^

Error: The specified module could not be found.

I've installed both those things as you've said! my pkg version is 5.5.2, node version: "16", windows x64, what exactly steps that I've to follow after installing those things you've specified?

@BlitzKiiro
Copy link

I kinda found some work around this problem by excluding the module from the bundled .exe file.

What I did was to require the canvas module with its absolute path like this:

const Canvas = require(path.join(process.execPath, "../canvas"));

then copying the Canvas module folder form node_modules and pasting it in the same directory with the final executable .exe file.

@luliloveshe
Copy link

So right after I posted my previous message, I ran Dependency Walker on the .node file and noticed some native Windows API's were missing. I just "guessted" a solution and went ahead and installed both .net runtime and vc redist (just google them) and it magically worked.

Just leaving this here for others that may get stuck on the ERR_DLOPEN_FAILED messages.

Links:

Note that your customers machines may need these too if they aren't installed.

I followed the way you said and found that the .node file depends on vcruntime140.dll. I am using windows server 2012R2 operating system. After installing VC++2015, it was successful.

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

No branches or pull requests

10 participants