strapi项目 yarn 报错:/node_modules/sharp: Command failed.

部署了strapi的服务器内存满了,优化清空的时候删了些东西,最后发现,竟导致整个Strapi项目构建失败,最后发现是因为删除了/root/.npm导致的。

问题现场:删除.npm后的灾难性报错

当在服务器上执行完rm -rf ~/.npm以清理磁盘空间,过了几天Strapi项目修改需要重启的时候,在项目中运行yarn install,终端突然抛出一连串红色错误:

bash

复制

下载

less 复制代码
"@strapi/strapi > @strapi/admin > use-context-selector@1.4.1" has unmet peer dependency "scheduler@>=0.19.0".
warning " > strapi-plugin-transformer@3.1.2" has unmet peer dependency "lodash@^4.17.21".
warning " > strapi-plugin-transformer@3.1.2" has unmet peer dependency "yup@^0.32.9".
warning " > styled-components@5.3.3" has unmet peer dependency "react-is@>= 16.8.0".
warning "styled-components > babel-plugin-styled-components > @babel/plugin-syntax-jsx@7.27.1" has unmet peer dependency "@babel/core@^7.0.0-0".
[5/5] Building fresh packages...
[6/10] ⠁ sharp
[10/10] ⠁ sqlite3
[8/10] ⠁ better-sqlite3
[9/10] ⠁ sharp
error /home/service/cms/web_tsinghua_cms/node_modules/@strapi/plugin-upload/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: /home/service/cms/web_tsinghua_cms/node_modules/@strapi/plugin-upload/node_modules/sharp
Output:
...

尽管网络通畅,sharp模块安装依然失败,导致整个Strapi项目无法启动。更令人困惑的是,同一命令在其它服务器却执行正常。


一、解剖问题:为什么删除.npm会引发sharp安装失败?

1. npm缓存的核心作用

~/.npm目录是npm的全局缓存仓库,存储着:

  • 下载过的所有包(_cacache目录)
  • 预编译的二进制模块(如sharp的本地编译版本)
  • 包元数据及签名信息8

当执行npm installyarn install时,系统优先检查缓存,若存在匹配版本则直接使用缓存包,避免重复下载和编译6。

2. Sharp模块的特殊性

Sharp是一个高性能图像处理库:

  1. 依赖预编译的C++库(libvips)
  2. 安装时会下载平台特定的二进制文件(Linux/macOS/Windows)
  3. 若缓存不存在,自动触发本地编译(需Python/C++编译环境)1

二、终极修复方案

直接从另一个服务器拷贝了一份具有相同环境的 /root/.npm,并且把服务的gcc、g++、glibc都重新升级以后好了。

相关推荐
likuolei18 小时前
XSL-FO 软件
java·开发语言·前端·数据库
正一品程序员18 小时前
vue项目引入GoogleMap API进行网格区域圈选
前端·javascript·vue.js
j***894618 小时前
spring-boot-starter和spring-boot-starter-web的关联
前端
star_111218 小时前
Jenkins+nginx部署前端vue项目
前端·vue.js·jenkins
im_AMBER18 小时前
Canvas架构手记 05 鼠标事件监听 | 原生事件封装 | ctx 结构化对象
前端·笔记·学习·架构
JIngJaneIL18 小时前
农产品电商|基于SprinBoot+vue的农产品电商系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·农产品电商系统
Tongfront18 小时前
前端通用submit方法
开发语言·前端·javascript·react
可爱又迷人的反派角色“yang”18 小时前
LVS+Keepalived群集
linux·运维·服务器·前端·nginx·lvs
han_18 小时前
前端高频面试题之CSS篇(二)
前端·css·面试
JIngJaneIL18 小时前
书店销售|书屋|基于SprinBoot+vue书店销售管理设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设·书店销售管理设计与实现