Skip to content

[Unity] Crash load Native .node library #471

Closed as not planned
Closed as not planned

Description

@Naminave

player.log

Editor.log

          (@"
            // globalThis.require = require('module').createRequire(process.execPath);
            const { os } = require('os');
            const { Hyperswarm } = require('hyperswarm');
            globalThis.getOsType = () => os.type();
          ");

Activity

  1. vmoroz commented on Nov 21, 2025

    @vmoroz
    Member

    @ImanCol , it is not clear from the issue description what you are trying to do and how to reproduce the issue.
    Thus, it is not actionable and it is difficult to help you.
    Could you please,

    • State what you are trying to do
    • What is the issue that you see
    • Provide the steps to reproduce the failing scenario

    In case if you see an issue with the microsoft/node-api-dotnet project, then please create an issue against that project and I will be glad to help you there.

  2. Naminave commented on Nov 21, 2025

    @Naminave
    Author

    @ImanCol , it is not clear from the issue description what you are trying to do and how to reproduce the issue. Thus, it is not actionable and it is difficult to help you. Could you please,

    • State what you are trying to do
    • What is the issue that you see
    • Provide the steps to reproduce the failing scenario

    In case if you see an issue with the microsoft/node-api-dotnet project, then please create an issue against that project and I will be glad to help you there.

    Oh, sorry, I've been reviewing several .NET projects based on Wrapp NodeLib, and I don't know how I got to this one.

    Perhaps I should even open the issue in the three most well-known bridges (Tencent/puerts, agracio/edge-js, Microsoft/node-api-dotne), or maybe just one in a central repository and mention all three.

    I should clarify that libnode in the three projects mentioned works fine with node.exe, whether from metacall/libnode or alshdavid/libnode-prebuilt for Windows compilation.

    What's the problem I'm having?

    Mainly, I can't load "nodes_modules" containing native .node or .bare files in these Wrapp .NET libnode projects. They immediately throw an exception or close (see the log files I sent).

    How to reproduce the problem?
    Just look for any native C++ library that works like nodes_modules (.node, .bare) or that depends on them (e.g., udx_native, sodium-native, keypear). Try referencing it in your main.js file, which will then be called in the C# script, using any libnode .NET wrapper.

    Does it load the NodeJS modules without problems?
    No.

    I've already opened a support issue for the "official" PuerTS example, but they refused to provide support.

    What's the point of allowing nodes_modules in these wrappers if most of them won't work?
    zombieyang/puerts-ts-loader#19 (comment)

  3. vmoroz commented on Nov 21, 2025

    @vmoroz
    Member

    Thanks! I cannot say much about other projects, but I will investigate the issue inside of the Microsoft code since I am one of the contributors there.

  4. Naminave commented on Nov 21, 2025

    @Naminave
    Author

    Oh, great, thanks. I also want to mention that I'm trying to use it in Unity. Here are some examples.

    Microsoft NodeApi Unity.zip

  5. vmoroz commented on Nov 21, 2025

    @vmoroz
    Member

    Great! Sorry, I will not be able to download your zip file.
    Giving a reference to some temporary repo usually works better.
    I assume I do not need the full unity context to see the issue.
    Which platform do you use? Windows? Linux?

    I guess the main issue is that each .node file expects Node-API functions to be exported from the executable file that you run.
    In case of libnode.dll these functions are exported from the DLL.
    The process must somehow re-export them in order to load .node extensions.
    If the process is your own then I would expect it can be done through a specially crafted .def file.
    In case if the process cannot be changed like with the ASP.NET server, then the problem becomes a bit more interesting.

  6. Naminave commented on Nov 22, 2025

    @Naminave
    Author

    I uploaded it to Gist.

    https://gist.github.com/ImanCol/eec07bae53020dbac2bd093ddb884592

    Just to find out why it was preventing me from loading libraries, I tried some other examples based on the DotNet Project (outside of Unity). Almost all of them gave me the same error: the application crashed when it tried to load them. I read somewhere that it might be related to CoreCLR (not yet implemented in Unity), but I don't know much about it. I only focus on the game engine, and the simple fact of having to compile such modules to be compatible sounds difficult to me, so I'm leaving this to someone with more experience because without any examples, I don't know what to do.

  7. Naminave commented on Nov 22, 2025

    @Naminave
    Author

    I'm only using Windows for now, but I want to make it work on Android, so the approach of using NodeJs as a process won't work. Therefore, I'll compile my game on Android with libnode when the modules don't crash the application.

  8. vmoroz commented on Nov 23, 2025

    @vmoroz
    Member

    @ImanCol , this is what I found so far:
    Similar issue was reported before: nodejs/node#47682
    Then, it was fixed in node-gyp and Node.js code in nodejs/node-gyp#2834
    Any .node module compiled by node-gyp (used by Node.js by default) is also linking the code from the src/win_delay_load_hook.cc.

    The issue with the .node modules that you had referenced is that they have their own version of that file that is not compatible with the libnode.dll support: https://github.com/holepunchto/cmake-napi/blob/main/win32/delay-load.c.

    The issue can be resolved by either changing the cmake-napi to provide libnode.dll-compatible version of delay-load.c, or you should consider a different module for your needs. Currently these .node modules cannot work with libnode.dll regardless if you use any .Net framework binding or not.

  9. Naminave commented on Nov 24, 2025

    @Naminave
    Author

    Oh, that's incredible. Give me some time to familiarize myself with CMake, and maybe I'll let the developers know if they'll provide support. I'll let you know here if anything changes. On another note, I'm glad LibNode supports .node, but to confirm, I'd like to try a native library that's already supported to see if my script doesn't encounter any errors (I'd even try it in PuerTS or EdgeJS).

  10. moved this from Need Triage to Has PR in Node-API Team Projecton Dec 5, 2025
  11. Naminave commented on Dec 11, 2025

    @Naminave
    Author

    I have partially solved this by replacing native libraries with .NET libraries using stub objects
    That way I don't have to worry about recompiling them (except for libnode and PuerTS) on different platforms, Unity will do it for me.

    UniTaN

    I'm sorry if this doesn't help anyone, I preferred this personal benefit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions