- 打包运行过程中的警告
Warning Cannot include directory %1 into executable.
The directory must be distributed with executable as %2.
%1: node_modules\puppeteer.local-chromium
%2: path-to-executable/puppeteer
解决方法:
警告大义为在路径如%1的某个目录、某个文件必须分配到exe文件的同一目录下,路径如%2,但是我按照warning手动复制目录、文件仍无效,如果有童鞋有效果!麻烦指导我一下!
最终我只能为使用到这些目录、文件的代码,指定使用路径,比如puppeteer,修改路径为:
executablePath: join(process.cwd(), 'puppeteer/win64-901912/chrome-win/chrome.exe')
因为pkg打包生成的exe运行后,会给__dirname等路径前加上一个临时目录:/snapshot/__dirname,而运行目录process.cwd()不会,所以拼接使用process.cwd();
xdg-open没有找到使用的地方,没管它也没报错;
- exe执行文件运行闪退
通过cmd界面,运行exe执行文件,可以看到错误信息显示如下:
pkg/prelude/bootstrap.js:1872
throw error;
Error: File ‘C:**\rh-design-print\node_modules\vm2\lib\setup-sandbox.js’ was not included into executable at compilation stage. Please recompile adding it as asset or script.
解决方法:
一开始我用了傻办法,把整个node_modules都打包进去,成功了,但是exe文件逼近1g,巨大无比;
后来发现,只把报错的这个库打包进去,也行,配置如下:
"pkg": {
"assets": [
"node_modules/vm2"
],
"outputPath": "dist/",
"targets": [
"node14-win-x64"
]
}
如果还是解决不了闪退问题,考虑把整个node_modules打包进去:
"pkg": {
"assets": [
"node_modules"
],
"outputPath": "dist/",
"targets": [
"node14-win-x64"
]
}