1

mac升级ventura系统之后electron项目打包报错问题

 1 year ago
source link: http://blog.xuedingmiao.com/blog/electron_build_error.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

记录下 electron 打包报错问题处理方法

# 背景

mac 升级了系统之后,之前的 electron 项目打包出现了异常,无法生成安装包:

⨯ Exit code: ENOENT. spawn /usr/bin/python ENOENT  failedTask=build stackTrace=Error: Exit code: ENOENT. spawn /usr/bin/python ENOENT
    at /Users/project/demo/node_modules/[email protected]@builder-util/src/util.ts:133:18
    at exithandler (child_process.js:326:5)
    at ChildProcess.errorhandler (child_process.js:338:5)
    at ChildProcess.emit (events.js:376:20)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
    at onErrorNT (internal/child_process.js:467:16)
    at processTicksAndRejections (internal/process/task_queues.js:82:21)

# 解决方法

# Python 依赖处理

首先打包是底层调用python来实现的所以先保证 python 是否正确安装。
执行python -V之后找不到命令,所以要重新安装一下。
下载地址:https://www.python.org/ftp/python/2.7.18/python-2.7.18-macosx10.9.pkg

安装好之后使用which python查看安装位置:

/Library/Frameworks/Python.framework/Versions/2.7/bin/python

但是装了之后仍然没有效果,原因是 Mac OS 在新系统中移除了 python2,旧系统中默认安装 python2,所以/usr/bin/python 默认可以调用 python2,而新系统中没有这个链接转而内置了/usr/bin/python3,打包过程中也是需要调用 /usr/bin/python,所以我们需要手动建立链接:

sudo ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/python /usr/bin/python

但是/usr/bin/这个目录做了系统级的保护,需要关闭 SIP 系统保护,太麻烦了(当然关闭系统保护之后添加链接也是可以的)。所以转而修改调用/usr/bin/python的地方。

# electron-builder依赖处理

打包使用的库是electron-builder,但是升级 builder 可能会引起一系列兼容问题,所以选择了不升级,修改本地 npm 依赖代码的方式修复。

这里使用的依赖版本是 22.14.13,找到报错的文件:

node_modules/electron-builder/node_modules/dmg-builder/out/dmg.js

大概 261 行的位置,将/usr/bin/python修改为/Library/Frameworks/Python.framework/Versions/2.7/bin/python

   await builder_util_1.exec(process.env.PYTHON_PATH || "/Library/Frameworks/Python.framework/Versions/2.7/bin/python", [path.join(dmgUtil_1.getDmgVendorPath(), "dmgbuild/core.py")], {
        cwd: dmgUtil_1.getDmgVendorPath(),
        env,
    });

保存之后就可以正常打包了。

# 参考资料


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK