Electron打包的时候路径存在中文出现的bug

发布时间 2023-05-26 12:08:31作者: NUNA11

在打包electron的时候报了这条错误

1 Error output:
2 !include: could not find: "C:\Users\xxxx(这里是中文)\AppData\Local\Temp\t-TH3KzB\0-messages.nsh"
3 Error in script "<stdin>" on line 75 -- aborting creation process

这个报错意思是路径有中文存在,解析不到,完整的错误显示如下:

 1 Error output:
 2 !include: could not find: "C:\Users\xxxx(这里是中文)\AppData\Local\Temp\t-TH3KzB\0-messages.nsh"
 3 Error in script "<stdin>" on line 75 -- aborting creation process
 4 
 5     at ChildProcess.<anonymous> (D:\electron-learing\electron-record-learn\record-project\node_modules\builder-util\src\util.ts:250:14)
 6     at Object.onceWrapper (node:events:642:26)                                                       s\builder-util\src\util.ts:250:14)
 7     at ChildProcess.emit (node:events:527:28)
 8     at ChildProcess.cp.emit (D:\electron-learing\electron-record-learn\record-project\node_modules\cross-spawn\lib\enoent.js:34:29)
 9     at maybeClose (node:internal/child_process:1092:16)
10     at Process.ChildProcess._handle.onexit (node:internal/child_process:302:5) {
11   exitCode: 1,
12   alreadyLogged: false,
13   code: 'ERR_ELECTRON_BUILDER_CANNOT_EXECUTE'
14 }

参考了网上的解决方法,思路是把编译出来的乱码用utf-8来编译:

找到 node_module/app-builder-lib/out/targets/nsis/NsisTarget.js文件,在 executeMakensis 方法中加入相应的代码,

 1 async executeMakensis(defines, commands, script) {
 2     const args = this.options.warningsAsErrors === false ? [] : ["-WX"];
 3     //添加下面这一行即可
 4     args.push("-INPUTCHARSET", "UTF8");
 5     for (const name of Object.keys(defines)) {
 6         const value = defines[name];
 7 
 8         if (value == null) {
 9             args.push(`-D${name}`);
10         } else {
11             args.push(`-D${name}=${value}`);
12         }
13     }
14 }

最后再重新build就能成功啦!