You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node v16中npm overrides未生效:sanitize-html版本未被正确覆盖

解决npm overrides未生效的问题

1. 修复package.json的语法错误

你提供的package.json中,dependencies块末尾缺少逗号,这会导致JSON解析失败,npm可能直接忽略overrides配置。修正后的配置如下:

{
  "dependencies": {
    "gatsby-transformer-remark":"5.24.0"
  },
  "overrides": {
    "gatsby-transformer-remark":{
      "sanitize-html":"2.7.3"
    }
  }
}

2. 清理现有依赖并重新安装

旧的缓存或lock文件可能导致覆盖规则不生效,执行以下步骤彻底重置依赖:

  • 删除node_modules文件夹:rm -rf node_modules(Windows系统使用rmdir /s /q node_modules)
  • 删除package-lock.json文件:rm package-lock.json(Windows系统使用del package-lock.json)
  • 清理npm缓存:npm cache clean --force
  • 重新安装依赖:npm install

3. 检查npm版本兼容性

overrides特性是从npm 8.3.0开始正式支持的,若你使用的npm版本低于此,配置不会生效。执行npm -v查看当前版本,版本过低则升级:

npm install -g npm@latest

4. 验证覆盖结果

重新安装后,执行以下命令确认sanitize-html的实际依赖版本:

npm ls sanitize-html

该命令会输出完整的依赖树,你可以直接查看gatsby-transformer-remark下的sanitize-html是否为指定的2.7.3版本。

如果以上步骤仍未解决问题,可尝试直接强制所有依赖使用指定版本的sanitize-html(注意:此方式可能引发其他兼容性问题,仅作为备选):

"overrides": {
  "sanitize-html": "2.7.3"
}

内容的提问来源于stack exchange,提问作者Mahendran V M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 23:15:29