asar 是一种将多个文件合并成一个文件的类 tar 风格的归档格式。 Electron 可以无需解压整个文件,即可从其中读取任意文件内容
参照如下步骤将你的应用打包成 asar归档文件
1. 安装 asar 实用程序
npm install -g asar
2. 使用 asar pack 打包
asar pack your-app app.asar
使用 asar 档案文件,在 Electron 中有两类 APIs:Node.js 提供的 Node API 和 Chromium 提供的 Web API。 这两种 API 都支持从 asar 档案中读取文件。
asar加密依赖第三方库执行报错
当你使用asar加密打报时,调用了第三方的so库,并且改库也在asar内,会有一下报错。
Error: libhello.so: cannot open shared object file: No such file or directory
at process.func [as dlopen] (node:electron/js2c/asar_bundle:5:1800)
at Object.Module._extensions..node (node:internal/modules/cjs/loader:1170:18)
at Object.func [as .node] (node:electron/js2c/asar_bundle:5:2027)
at Module.load (node:internal/modules/cjs/loader:982:32)
at Module._load (node:internal/modules/cjs/loader:823:12)
at Function.c._load (node:electron/js2c/asar_bundle:5:13331)
at Module.require (node:internal/modules/cjs/loader:1006:19)
at require (node:internal/modules/cjs/helpers:93:18)
at new Log (/usr/lib/helloworld/resources/app.asar/packages/main/dist/index.js:1:301)
at /usr/lib/helloworld/resources/app.asar/packages/main/dist/index.js:1:1228606 {
code: 'ERR_DLOPEN_FAILED'
}
A JavaScript error occurred in the main process
Uncaught Exception:
TypeError: t is not a constructor
at new Log (/usr/lib/helloworld/resources/app.asar/packages/main/dist/index.js:1:354)
at /usr/lib/helloworld/resources/app.asar/packages/main/dist/index.js:1:1228606
at /usr/lib/helloworld/resources/app.asar/packages/main/dist/index.js:1:1247263
at Object.<anonymous> (/usr/lib/helloworld/resources/app.asar/packages/main/dist/index.js:1:1247267)
at Module._compile (node:internal/modules/cjs/loader:1110:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1140:10)
at Module.load (node:internal/modules/cjs/loader:982:32)
at Module._load (node:internal/modules/cjs/loader:823:12)
at Function.c._load (node:electron/js2c/asar_bundle:5:13331)
at Object.<anonymous> (node:electron/js2c/browser_init:197:3197)