Node.js 如何确定已编译本机模块的ABI版本(以及其他详细信息)?
我正在合作开发一个VSCode扩展,其中使用了本机模块(serialport)。 为了使解决方案在所有平台上稳定运行,并且随着VScode版本的变化,我希望包含预构建本机模块 现在看来,这些预编译模块中的一些(不是全部)并不是它们声称的版本 对于测试/验证,我希望以编程方式确定ABI版本,如果可能的话,确定install prebuild下载的每个本机模块的运行时、平台(darwin、linux、win32)和体系结构 ie当我尝试在electron 5.0.10(ABI-70)中加载模块时引发以下错误: 因此,当它作为ABI70通过预构建安装下载时,它报告了ABI47。 注意:将电子绑定存储在节点绑定位置,以允许“绑定”检测ABI、平台和体系结构Node.js 如何确定已编译本机模块的ABI版本(以及其他详细信息)?,node.js,visual-studio-code,electron,vscode-extensions,node-serialport,Node.js,Visual Studio Code,Electron,Vscode Extensions,Node Serialport,我正在合作开发一个VSCode扩展,其中使用了本机模块(serialport)。 为了使解决方案在所有平台上稳定运行,并且随着VScode版本的变化,我希望包含预构建本机模块 现在看来,这些预编译模块中的一些(不是全部)并不是它们声称的版本 对于测试/验证,我希望以编程方式确定ABI版本,如果可能的话,确定install prebuild下载的每个本机模块的运行时、平台(darwin、linux、win32)和体系结构 ie当我尝试在electron 5.0.10(ABI-70)中加载模块时引发
## npx prebuild-install
Download prebuild native binding for runtime electron : 5.0.5, abi: 70, win32, x64
prebuild-install info begin Prebuild-install version 5.3.0
prebuild-install info looking for cached prebuild @ C:\Users\josverl\AppData\Roaming\npm-cache\_prebuilds\bde028-bindings-v2.0.8-electron-v70-win32-x64.tar.gz
prebuild-install info found cached prebuild
prebuild-install info unpacking @ C:\Users\josverl\AppData\Roaming\npm-cache\_prebuilds\bde028-bindings-v2.0.8-electron-v70-win32-x64.tar.gz
prebuild-install info unpack resolved to C:\develop\NodeJS\electron-serialport\node_modules\@serialport\bindings\build\Release\bindings.node
prebuild-install info install Successfully installed prebuilt binary!
Copy to :
-> C:\develop\NodeJS\electron-serialport\noded_modules\@serialport\bindings\lib\binding\node-v70-win32-x64\bindings.node
process.versions.modules
应该为给定节点或运行时获取正确的ABI
您自己的解决方案是在不添加本机节点模块的情况下执行此操作的唯一方法,并且解析/读取.node
文件不会直接将信息作为字符串或int值(使用十六进制编辑器检查)
可能的原因是:
- 您应该确保模块的安装过程不会触发本地模块的构建,例如:运行
而不使用npm install
,因为如果包含的包具有自定义安装脚本,则使用本地安装的节点版本来重建任何依赖性--忽略脚本。
- 您的预构建被其他安装步骤忽略
- 包已将错误的
文件作为预生成上载.node
mp->nm\u version
,但仅通过错误报告
所以到目前为止找到的唯一方法就是解析错误消息。限制是,这只能在与当前运行时相同的平台和CPU体系结构上工作,但总比什么都不工作要好
下面将通过捕获错误消息并从中提取ABI来提取本机模块的ABI版本
// try to determine the ABI version for a native module
const getNativeABI = (filename) => {
var moduleVersion = 0
try {
var test = new Module(filename, null);
process.dlopen(module, filename) //,os.constants.dlopen.RTLD_NOW);
// if this works the node version is the same
moduleVersion = process.versions['modules']
// but now we need to unload it :-(
return moduleVersion
} catch (error) {
var match
var versionRegexp = /NODE_MODULE_VERSION (\d*)./gm
var platformRegexp = /(is not a valid Win32 application|invalid ELF header|wrong ELF class)/g
// check for ABI version mismatch
// Uncaught Error: The module '..\bindings.node'
// was compiled against a different Node.js version using
// NODE_MODULE_VERSION 47. This version of Node.js requires
// NODE_MODULE_VERSION 70. Please try re-compiling or re-installing
match = versionRegexp.exec(error.message)
if (match != null){
return match[1] // first version is that of the module
}
// not for valid on this win32 / linux
match = platformRegexp.exec(error.message)
if (match != null){
// todo: @linux : use error for elfclass to determine architecture :: wrong ELF class: ELFCLASS32
return 0 // can't validate cross platform
}
// other error
console.debug( error.message)
}
return moduleVersion // just in case
}
您需要传入一个虚拟模块结构
/// dummy copy of internal function
function Module(id, parent) {
this.id = id;
this.exports = {};
this.parent = parent;
// updateChildren(parent, this, false);
this.filename = null;
this.loaded = false;
this.children = [];
}
不,这根本不是问题所在。一方面,假定一个软件的任何和所有用户都有能力从C/C++源编译本地模块。假定安装预构建是为了防止需要为源构建,因为原始电子作者5.0.10(ABI-70):模块,并且提供了下载。但是交付的二进制文件应该是正确的ABI版本,而事实似乎并非如此。alsoas指定这是一个,最后,我想获得一个“待加载模块”的ABI,而不是运行时的ABI。从我看到的情况来看,serialport有一个问题,可能缓存的
节点文件包含错误的构建。换句话说,假设“在手动安装软件包后,您的下载仍然必须被覆盖,否则您将无法加载ABI 47。”这被证明是不正确的。不过,感谢另一个问题,我忽略了这一点,但事实上是同一个问题。
/// dummy copy of internal function
function Module(id, parent) {
this.id = id;
this.exports = {};
this.parent = parent;
// updateChildren(parent, this, false);
this.filename = null;
this.loaded = false;
this.children = [];
}