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.