M1 Mac使用Yarn安装依赖时Sharp包安装失败求助
解决M1 Max macOS 12.5下Sharp包安装失败问题
问题背景
搭载M1 Max芯片的macOS 12.5系统中,使用yarn安装项目依赖时,Sharp包始终安装失败。已尝试通过brew install libvips安装依赖库,但问题未解决。
项目依赖配置
{ "name": "@mike-north/typescript-fundamentals-v3", "version": "0.0.1", "description": "Mike's TypeScript Fundamentals Course (v3)", "main": "index.js", "repository": "git@github.com:mike-north/ts-fundamentals-v3.git", "author": "Mike North <michael.l.north@gmail.com>", "license": "NOLICENSE", "private": true, "workspaces": [ "packages/*" ], "volta": { "node": "14.7.0", "yarn": "1.22.10" }, "devDependencies": { "@babel/core": "^7.4.0-0", "@babel/preset-typescript": "^7.16.7", "core-js": "^3.0.0", "lerna": "^4.0.0", "replace-in-file": "^6.2.0", "webpack": "^5.37.0" }, "scripts": { "postinstall": "replace-in-file 'import chalk' 'import * as chalk' node_modules/gatsby-cli/lib/reporter/reporter.d.ts", "dev-website": "lerna run dev --scope website --stream", "dev-hello-ts": "lerna run dev --scope hello-ts --stream" }, "dependencies": {} }
错误日志
error /Users/myname/Documents/Projects/ts-fundamentals-v3/node_modules/sharp: Command failed. Exit code: 1 Command: (node install/libvips && node install/dll-copy && prebuild-install) || (node install/can-compile && node-gyp rebuild && node install/dll-copy) Arguments: Directory: /Users/myname/Documents/Projects/ts-fundamentals-v3/node_modules/sharp Output: sharp: Using cached /Users/myname/.npm/_libvips/libvips-8.10.6-darwin-arm64v8.tar.br prebuild-install WARN install No prebuilt binaries found (target=3 runtime=napi arch=arm64 libc= platform=darwin) gyp info it worked if it ends with ok gyp info using node-gyp@7.1.2 gyp info using node@18.7.0 | darwin | arm64 gyp info find Python using Python version 3.8.9 found at "/Library/Developer/CommandLineTools/usr/bin/python3" (node:23828) [DEP0150] DeprecationWarning: Setting process.config is deprecated. In the future the property will be read-only. (Use `node --trace-deprecation ...` to show where the warning was created) gyp info spawn /Library/Developer/CommandLineTools/usr/bin/python3 gyp info spawn args [ gyp info spawn args '/Users/myname/Documents/Projects/ts-fundamentals-v3/node_modules/@npmcli/run-script/node_modules/node-gyp/gyp/gyp_main.py', gyp info spawn args 'binding.gyp', gyp info spawn args '-f', gyp info spawn args 'make', gyp info spawn args '-I', gyp info spawn args '/Users/myname/Documents/Projects/ts-fundamentals-v3/node_modules/sharp/build/config.gypi', gyp info spawn args '-I', gyp info spawn args '/Users/myname/Documents/Projects/ts-fundamentals-v3/node_modules/@npmcli/run-script/node_modules/node-gyp/addon.gypi', gyp info spawn args '-I', gyp info spawn args '/Users/myname/Library/Caches/node-gyp/18.7.0/include/node/common.gypi', gyp info spawn args '-Dlibrary=shared_library', gyp info spawn args '-Dvisibility=default', gyp info spawn args '-Dnode_root_dir=/Users/myname/Library/Caches/node-gyp/18.7.0', gyp info spawn args '-Dnode_gyp_dir=/Users/myname/Documents/Projects/ts-fundamentals-v3/node_modules/@npmcli/run-script/node_modules/node-gyp', gyp info spawn args '-Dnode_lib_file=/Users/myname/Library/Caches/node-gyp/18.7.0/<(target_arch)/node.lib', gyp info spawn args '-Dmodule_root_dir=/Users/myname/Documents/Projects/ts-fundamentals-v3/node_modules/sharp', gyp info spawn args '-Dnode_engine=v8', gyp info spawn args '--depth=.', gyp info spawn args '--no-parallel', gyp info spawn args '--generator-output', gyp info spawn args 'build', gyp info spawn args '-Goutput_dir=.' gyp info spawn args ] gyp info spawn make gyp info spawn args [ 'BUILDTYPE=Release', '-C', 'build' ] CC(target) Release/obj.target/nothing/../node-addon-api/nothing.o LIBTOOL-STATIC Release/nothing.a env: python: No such file or directory make: *** [Release/nothing.a] Error 127 gyp ERR! build error gyp ERR! stack Error: `make` failed with exit code: 2 gyp ERR! stack at ChildProcess.onExit (/Users/myname/Documents/Projects/ts-fundamentals-v3/node_modules/@npmcli/run-script/node_modules/node-gyp/lib/build.js:194:23) gyp ERR! stack at ChildProcess.emit (node:events:513:28) gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:291:12) gyp ERR! System Darwin 21.6.0 gyp ERR! command "/opt/homebrew/Cellar/node/18.7.0/bin/node" "/Users/myname/Documents/Projects/ts-fundamentals-v3/node_modules/.bin/node-gyp" "rebuild" gyp ERR! cwd /Users/myname/Documents/Projects/ts-fundamentals-v3/node_modules/sharp gyp ERR! node -v v18.7.0
系统环境信息
System: OS: macOS 12.5 CPU: (10) arm64 Apple M1 Max Memory: 108.36 MB / 32.00 GB Shell: 5.8.1 - /bin/zsh Binaries: Node: 18.7.0 - /opt/homebrew/bin/node Yarn: 1.22.19 - /opt/homebrew/bin/yarn npm: 8.15.0 - /opt/homebrew/bin/npm
关键错误分析
- Node版本不匹配:项目
volta配置指定Node版本为14.7.0,但当前系统使用的是18.7.0,导致Sharp无法找到对应架构的预编译二进制包,触发源码编译流程。 - Python命令缺失:编译过程中提示
env: python: No such file or directory,macOS 12+默认仅提供python3命令,无python软链接,导致make执行失败。
解决方案
1. 对齐项目指定的Node/Yarn版本
项目已配置volta,确保volta已安装后,在项目根目录执行:
volta install
此命令会自动切换到package.json中指定的Node 14.7.0和Yarn 1.22.10,解决版本不兼容导致的预编译包缺失问题。
2. 修复Python命令缺失问题
执行以下命令创建python到python3的软链接:
ln -s /usr/bin/python3 /usr/local/bin/python
或通过Homebrew安装Python并自动配置软链接:
brew install python
3. 清理缓存并重新安装依赖
删除现有依赖和锁文件:
rm -rf node_modules yarn.lock
重新执行安装:
yarn install
若仍存在问题,可强制Sharp使用系统已安装的libvips:
SHARP_IGNORE_GLOBAL_LIBVIPS=0 yarn install
内容的提问来源于stack exchange,提问作者madfcat
相关产品推荐
相关产品推荐

