使用 require() 加载插件


已编译的插件二进制文件的文件扩展名是 .node(与 .dll.so 相反)。 require() 函数用于查找具有 .node 文件扩展名的文件并将它们初始化为动态链接库。

调用 require() 时,通常可以省略 .node 扩展名,Node.js 仍会找到并初始化插件。 但是,有一个注意事项,Node.js 将首先尝试定位和加载碰巧共享相同基本名称的模块或 JavaScript 文件。 例如,如果在与二进制 addon.node 相同的目录中有一个文件 addon.js,那么 require('addon') 将优先于 addon.js 文件并加载它。

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.