将Node.js从14升级到22时遭遇libxslt与node-gyp报错
问题:Node.js 14升级至22.3.0后npm install触发libxslt与node-gyp编译失败
环境信息
- Linux(x64)系统
- Node.js版本:22.3.0
- npm版本:9.3.0
- Python版本:3.8.10(已确认被node-gyp调用)
已尝试操作
- 将node-gyp升级至10版本后又降级回9版本
- npm install过程已触发依赖重建,但问题未解决
报错信息
npm ERR! code 1
npm ERR! path /path/to/project/node_modules/libxslt
npm ERR! command failed
npm ERR! command sh -c node-gyp rebuild
make: Entering directory '/path/to/project/node_modules/libxslt/build'
CC(target) Release/obj.target/libexslt/deps/libxslt/libexslt/common.o
...
npm ERR! ../deps/libxslt.config/linux/x64/config.h:53: warning: "HAVE_LIBPTHREAD" redefined
...
npm ERR! ../deps/libxslt/libexslt/functions.c:224: warning: cast between incompatible function types
...
npm ERR! gyp ERR! stack Error:makefailed with exit code: 2
解决方案
1. 安装系统级依赖库
libxslt编译依赖系统底层库,根据发行版执行对应命令:
# Debian/Ubuntu系 sudo apt-get install libxslt1-dev libxml2-dev # RHEL/CentOS系 sudo yum install libxslt-devel libxml2-devel
2. 锁定node-gyp兼容版本
使用与Node.js 22匹配的node-gyp版本:
npm install -g node-gyp@10.1.0
然后在项目目录强制重建libxslt:
npm rebuild libxslt --force
3. 清理缓存后重装
npm cache clean --force rm -rf node_modules package-lock.json npm install
4. 更新libxslt包版本
当前libxslt版本可能不兼容Node.js 22,尝试升级至最新兼容版:
npm install libxslt@latest --save
内容的提问来源于stack exchange,提问作者Anubhav
相关产品推荐
相关产品推荐

