如何在Digital Ocean Ubuntu 20 Droplet上运行Strapi并修复sharp模块安装错误?
修复Strapi在Digital Ocean Ubuntu Droplet上的Sharp模块启动错误
这个问题很常见——sharp是依赖系统架构的二进制图像处理库,你在Mac(Darwin/x64架构)上安装的sharp二进制文件,和Ubuntu(Linux/x64)的完全不兼容;就算没把node_modules传到GitHub,也可能因为Ubuntu缺少sharp编译所需的系统依赖,导致安装失败。
按照下面的步骤一步步解决:
1. 确保GitHub仓库不包含node_modules(关键前置操作)
如果之前不小心把Mac上的node_modules推到了GitHub,先在本地处理:
- 删除本地项目的
node_modules和package-lock.json - 更新
.gitignore文件,添加一行:node_modules/ - 重新提交并推送代码:
git add .gitignore git commit -m "Exclude node_modules from repo" git push origin main
2. 在Droplet上清理残留依赖文件
进入你的项目目录,删除可能存在的旧依赖文件:
rm -rf node_modules package-lock.json
3. 安装Sharp所需的系统底层依赖
Ubuntu 20.04需要先安装编译sharp的基础库,否则npm无法成功编译Linux版本的sharp:
sudo apt-get update && sudo apt-get install -y libvips-dev libjpeg-dev libpng-dev libwebp-dev
4. 重新安装项目依赖
现在执行npm install,系统依赖齐全后,会自动编译适配Linux的sharp:
npm install
如果还是报错,直接指定平台安装sharp:
npm install --platform=linux --arch=x64 sharp
5. 启动Strapi
最后尝试启动开发服务器:
npm run develop
这样应该就能正常启动了。另外提醒下,生产环境部署建议使用npm run start命令,且Droplet至少需要1GB内存(推荐2GB)才能稳定运行Strapi。
内容的提问来源于stack exchange,提问作者preston
相关产品推荐
相关产品推荐

