javascript 如何使用 .node 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30249234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use a .node file?
提问by idude
I was trying to install node_mouseand 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)
我试图安装node_mouse,当我查看我的节点模块文件夹时,我找到了一个 .node 文件扩展名,而不是普通的 .js 文件扩展名。我怎么能运行 node_mouse?我查了一下,我认为它可能是用 C++ 编写的插件,但我不确定(节点插件)
回答by Hyman Wade
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:
是的,正常的“require”用法适用于 .node 文件。这些文件的目的是创建可移植的二进制文件(使用 node-gyp,来自 C++),可以像普通节点一样被引用。请参阅节点插件文档的 hello.js 部分:
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):
在查看这个 NPM 库后,它在我的 Windows、Mac 和 Linux VM 上正确加载到具有多个不同节点版本的节点上,但二进制文件引发了一系列错误。在 windows 上,它有一个特定版本的 windows 作为构建目标(可能是 NT,因为 windows 10 会引发错误):
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):
在 OS X 上,这是 dyld 无法打开二进制文件引用的共享库。(参见 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.
在 Linux 上,我们收到一个 ELF 标头错误,它告诉我们二进制文件无法在此操作系统上运行。
Error: /app/available_modules/1484064894000/node_mouse/node_mouse.node: invalid ELF header
The authorseems 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.
在笔者看来,做了很多的Windows NT工作,所以如果你真的需要这方面的工作,找到Windows NT的全新副本的所有开发附加功能。
Lastly, consider the security risk of running third-party closed source binaries in your code base (especially ones that control mouse movement).
最后,考虑在您的代码库中运行第三方闭源二进制文件的安全风险(尤其是那些控制鼠标移动的)。
回答by ChrisM
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.
是的,这些 .node 文件是 Node Addons(二进制模块),您应该可以直接使用require()
它们。请注意,它将首先查找.json
和.js
文件。
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.
已编译的 Addon 二进制文件的文件扩展名是 .node(与 .dll 或 .so 相反)。编写 require() 函数以查找具有 .node 文件扩展名的文件并将其初始化为动态链接库。
调用 require() 时,通常可以省略 .node 扩展名,Node.js 仍会找到并初始化插件。然而,一个警告是 Node.js 将首先尝试定位和加载碰巧共享相同基本名称的模块或 JavaScript 文件。例如,如果在与二进制 addon.node 相同的目录中有一个文件 addon.js,那么 require('addon') 将优先加载 addon.js 文件并加载它。
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.
您还应该知道这些是二进制模块,因此加载它们很像运行标准的可执行文件(.exe
如果您熟悉 Windows,请考虑文件)。与本机可执行文件一样,它们更依赖于系统的详细信息,并且还可能存在安全风险。虽然标准.js
模块是可移植的(有一些警告),但.node
二进制模块将从根本上为特定的机器架构和操作系统构建,通常甚至是特定版本的 Node.js。如果您在加载二进制模块时遇到问题,您应该确保您正在为您的系统运行正确的版本,并与供应商确认您的系统确实受支持。
Sometimes specific functionality or performance needs requires it but with Node.js you shouldn't be loading binary modules unless you really have to.
有时特定的功能或性能需要需要它,但使用 Node.js 你不应该加载二进制模块,除非你真的必须这样做。