使用 require() 加载插件
【Loading addons using require()】
已编译的插件二进制文件的文件名扩展名是 .node(而不是 .dll 或 .so)。require() 函数被编写为查找具有 .node 文件扩展名的文件,并将其初始化为动态链接库。
【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.】
在调用 require() 时,通常可以省略 .node 扩展名,Node.js 仍然能够找到并初始化该插件。然而,有一个注意事项是,Node.js 会先尝试定位并加载那些恰好具有相同基本名称的模块或 JavaScript 文件。例如,如果在与二进制文件 addon.node 相同的目录中有一个文件 addon.js,那么 require('addon') 会优先加载 addon.js 文件,而不是 addon.node。
【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.】