最近 开发 electron 项目需要使用到 robotjs 这个包,安装阶段就一直出现问题
报错
npm WARN deprecated npmlog@4.1.2: This package is no longer supported.
npm WARN deprecated are-we-there-yet@1.1.7: This package is no longer supported.
npm WARN deprecated gauge@2.7.4: This package is no longer supported.
npm ERR! code 1
npm ERR! path /Users/xxx/release/app/node_modules/robotjs
npm ERR! command failed
npm ERR! command sh -c prebuild-install || node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@9.4.1
npm ERR! gyp info using node@18.18.0 | darwin | arm64
npm ERR! gyp info find Python using Python version 3.12.5 found at "/opt/homebrew/opt/python@3.12/bin/python3.12"
npm ERR! gyp info spawn /opt/homebrew/opt/python@3.12/bin/python3.12
npm ERR! gyp info spawn args [
npm ERR! gyp info spawn args '/Users/xxxx/node_modules/node-gyp/gyp/gyp_main.py',
npm ERR! gyp info spawn args 'binding.gyp',
...
省略报错
...
npm ERR! Traceback (most recent call last):
npm ERR! File "/Users/xxxx/node_modules/node-gyp/gyp/gyp_main.py", line 42, in <module>
npm ERR! import gyp # noqa: E402
npm ERR! ^^^^^^^^^^
npm ERR! File "/Users/xxxx/node_modules/node-gyp/gyp/pylib/gyp/__init__.py", line 9, in <module>
npm ERR! import gyp.input
npm ERR! File "/Users/xxxx/node_modules/node-gyp/gyp/pylib/gyp/input.py", line 19, in <module>
npm ERR! from distutils.version import StrictVersion
npm ERR! ModuleNotFoundError: No module named 'distutils'
npm ERR! gyp ERR! configure error
...
省略报错
...
npm ERR! gyp ERR! node -v v18.18.0
npm ERR! gyp ERR! node-gyp -v v9.4.1
npm ERR! gyp ERR! not ok
npm ERR! A complete log of this run can be found in: /Users/xxxx/.npm/_logs/2024-08-26T03_37_31_434Z-debug-0.log
问题分析:
robotjs 安装完成后会使用 node-gyp 重新构建成 .node 文件供 electron 项目使用
此处出现 node-gyp python 相关报错
原因分析:
从报错来看 node-gyp 引入 distutils 是报错, 重要提示
npm ERR! ModuleNotFoundError: No module named 'distutils'
查看文档得知 Python 从 12.0 版本后移除了 distutils 参考: Python 3.12 有什么新变化
解决方案:
- 安装旧版本 Python, 我这里使用 v3.10.14 没有出现问题
- 重新安装 robotjs