javascript - 如何使用 .node 文件?

标签 javascript node.js npm node-gyp

我正在尝试安装 node_mouse当我查看我的 Node 模块文件夹而不是普通的 .js 文件扩展名时,我发现了一个 .node 文件扩展名。我怎么能运行 node_mouse?我查了一下,我认为它可能是一个用 C++ 编写的插件,但我不太确定(Node addons)

最佳答案

是的,这些 .node 文件是 Node Addons(二进制模块),您应该可以在它们上使用 require()。请注意,它将首先查找 .json.js 文件。

来自 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.

您还应该知道这些是二进制模块,因此加载它们很像运行一个标准的可执行文件(如果您只是熟悉 Windows,可以考虑一下 .exe 文件)。与 native 可执行文件一样,它们更多地依赖于您系统的细节,并且还可能存在安全风险。虽然标准的 .js 模块是可移植的(有一些注意事项),但 .node 二进制模块将从根本上为特定的机器架构和操作系统甚至通常是特定的 Node 版本。如果您在加载二进制模块时遇到问题,您应该确保您运行的是适合您系统的正确版本,并与 vendor 确认您的系统实际上是受支持的。

有时特定功能或性能需要需要它,但使用 Node.js 时,除非确实需要,否则不应加载二进制模块。

PS:这是一个老问题和答案,但我碰巧遇到了一些让我想起了这一点的东西,所以记录一下:不要使用看起来像垃圾包的 node_mouse,而是global-mouse-events看起来更有希望。

关于javascript - 如何使用 .node 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30249234/

相关文章:

node.js - 在 Mongoose 中跨整个应用程序共享数据库连接

javascript - 如何在 UIAutomation 中使用 Node 模块

angular - 如何导入 bootstrap 3.3.7 模块 - 在 angular2 组件中使用

javascript - 为什么 chrome 后退/前进按钮不触发 popstate 事件?

php - Facebook 登录成功后重新加载页面

javascript 使用数组交换变量

node.js - 在快速中间件内没有调用Sinon spy

javascript - 在指令模板中使用 Angular 形式

node.js - 避免 Node.js Web 应用程序中的竞争条件

javascript - 设置 CSS 模块时 webpack 中的配置对象无效