当您在应用程序中包含本机模块时,可能会发生这种错误。本机模块需要针对构建应用程序的操作系统特定节点进行编译。因此,当您压缩到 ASAR 时,您必须排除项目中的任何本机模块。这是通过在整个打包程序选项中的 'asar' 选项中的 unpackDir 选项完成的。例如,在我的 gulp 构建中,我执行以下操作:
var gulp = require('gulp');
var $ = require('gulp-load-plugins')({lazy: true});
var config = require('./gulp.config')();
var packager = require('electron-packager');
var electronPackage = require('electron/package.json');
var electronVersion = electronPackage.version;
var pkg = require('./package.json');
// Build the electron app
gulp.task('build:electron', function(cb) {
var opts = {
name: pkg.name,
platform: 'win32',
arch: 'ia32', // ia32, x64 or all
dir: config.root, // source location of app
out: config.electronbuild, // destination location for app os/native binaries
ignore: config.electronignore, // don't include these directories in the electron app build
icon: config.icon,
asar: {unpackDir: config.electroncompiled}, // compress project/modules into an asar blob but don't use asar to pack the native compiled modules
overwrite: true,
prune: true,
electronVersion: electronVersion, // Tell the packager what version of electron to build with
'app-copyright': pkg.copyright, // copyright info
'app-version': pkg.version, // The version of the application we are building
win32metadata: { // Windows Only config data
CompanyName: pkg.authors,
ProductName: pkg.name,
FileDescription: pkg.description,
OriginalFilename: pkg.name + '.exe'
}
};
packager(opts, function(err, appPath) {
$.util.log(' <- packagerDone()', err, appPath);
log(' all done!');
cb();
});
});
对您的情况重要的是 asar 选项的行。您要确保执行类似 的操作asar: {unpackDir: config.electroncompiled}
,并简单地将config.electroncompiled
变量替换为包含项目使用的任何本机编译包的目录文件 glob(例如libxml-xsd
,libxmljs-mt
等nslog
)。希望这是有道理的。