How to use a .node file?

29,230

Solution 1

Yep these .node files are Node Addons (binary modules) and you should be able to just use require() on them. Be aware that it will look for .json and .js files first.

From the documentation:

The filename extension of the compiled Addon binary is .node (as opposed to .dll or .so). The require() function is written to look for files with the .node file extension and initialize those as dynamically-linked libraries.

When calling require(), the .node extension can usually be omitted and Node.js will still find and initialize the Addon. One caveat, however, is that Node.js will first attempt to locate and load modules or JavaScript files that happen to share the same base name. For instance, if there is a file addon.js in the same directory as the binary addon.node, then require('addon') will give precedence to the addon.js file and load it instead.

You should also be aware that these are binary modules, so loading them is a lot like just running a standard executable file (Think .exe file if you are just familiar with Windows). Like native executables they are a lot more dependent on the particulars of your system and also potentially a security risk. While a standard .js module will be portable (with a few caveats) a .node binary module will be fundamentally built for a particular machine architecture and OS and often even a particular version of Node. If you are having trouble loading a binary module you should make sure you are running the right version for your system, and confirm with the provider that your system is actually supported.

Sometimes specific functionality or performance needs requires it but with Node.js you shouldn't be loading binary modules unless you really have to.

Solution 2

Yes, the normal "require" usage is appropriate for .node files. The point of these files is to create portable binaries (using node-gyp, from C++) that can be referenced like normal node requires. See the hello.js section of the node addon docs:

const addon = require('./build/Release/addon');

console.log(addon.hello());

After looking into this NPM lib, it is loaded by node correctly on my Windows, Mac, and Linux VM's with several different node versions, but the binary throws an array of errors. On windows, it has a specific version of windows as a build target (likely NT, because windows 10 throws an error):

Error: %1 is not a valid Win32 application.

On OS X, this is dyld failing to open a shared library referenced by the binary. (See man dlopen):

Error:dlopen(/.../node_mouse/node_mouse.node, 1): no suitable image found. 

On Linux, we get an ELF header error, which tells us that the binary can't be run on this OS.

Error: /app/available_modules/1484064894000/node_mouse/node_mouse.node: invalid ELF header

The author seems to do a lot of Windows NT work, so if you really need this working, find a fresh copy of Windows NT with all the dev add ons.

Lastly, consider the security risk of running third-party closed source binaries in your code base (especially ones that control mouse movement).

Share:
29,230

Related videos on Youtube

idude
Author by

idude

Updated on July 09, 2022

Comments

  • idude
    idude almost 2 years

    I was trying to install node_mouse and when I looked in my node modules folder and instead of a normal .js file extension, I found a .node file extension. How could I run node_mouse? I looked this up and I think it might be an addon written in C++, but I'm not exactly sure(Node addons)

    • loganfsmyth
      loganfsmyth almost 9 years
      You shouldn't need to think about extensions, just require the module like any other module. Are you getting some error?
    • idude
      idude almost 9 years
      I was, but not I realized I didn't have a proper C++ compiler, working on that right now.
    • Asif Ali
      Asif Ali about 8 years
      I am getting some error with .node file extension as well, can you give me a clue how did you get it working ?
    • Terra Ashley
      Terra Ashley over 7 years
      Also having issues. Please advise.
    • Vaibhav N Naik
      Vaibhav N Naik over 7 years
      Facing same issue with require() on a file with .node extension
  • ChrisM
    ChrisM almost 6 years
    The comment "On windows, it has a specific version of windows as a build target (likely NT, because windows 10 throws an error)" doesn't make sense because Windows 10 is an instance of Windows NT. You may not realise but there is no single "Windows NT" that is a name that applies to all non DOS based versions of Windows, which includes all consumer, server and embedded versions from Windows XP onwards.
  • ChrisM
    ChrisM almost 6 years
    Also - The error message you get there is probably nothing to do with the version of Windows. It's not obvious why you get that, because there are a lot of reasons why something might not be a valid Win32 application.