javascript - what - How to use a.node file?

what is node.js used for (2)

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)

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.

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');


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).