Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a .node file?

Tags:

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)

like image 880
idude Avatar asked May 14 '15 23:05

idude


People also ask

What is a .node file?

A NODE file contains an addon, which is a compiled library of functions used by one or more Node. js applications. It stores binary data compiled from a GYP file written in the C++ programming language. NODE files are similar to .

How do I open a node file?

If you cannot open your NODE file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a NODE file directly in the browser: Just drag the file onto this browser window and drop it.


1 Answers

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.

like image 100
ChrisM Avatar answered Sep 29 '22 23:09

ChrisM