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

Next.js项目更新依赖后ESLint格式化器加载失败求助

解决Next.js项目ESLint加载stylish formatter报错问题

报错信息

There was a problem loading formatter: /Users/naya/Desktop/work/ftl/ftl-web-fe/node_modules/eslint/lib/cli-engine/formatters/stylish
Error: require() of ES Module /Users/naya/Desktop/work/ftl/ftl-web-fe/node_modules/strip-ansi/index.js from /Users/naya/Desktop/work/ftl/ftl-web-fe/node_modules/eslint/lib/cli-engine/formatters/stylish.js not supported.
Instead change the require of index.js in /Users/naya/Desktop/work/ftl/ftl-web-fe/node_modules/eslint/lib/cli-engine/formatters/stylish.js to a dynamic import() which is available in all CommonJS modules.

解决方法

1. 锁定strip-ansi兼容版本

报错根源是strip-ansi更新为纯ES模块版本,而当前ESLint版本无法用require()加载它。可通过锁定该包的CommonJS兼容版本解决:

  • npm用户:
    1. 安装依赖工具:npm install -D npm-force-resolutions
    2. 在package.json中添加配置:
      "resolutions": {
        "strip-ansi": "^6.0.1"
      },
      "scripts": {
        "preinstall": "npx npm-force-resolutions"
      }
      
    3. 删除node_modules和package-lock.json,重新执行npm install
  • yarn用户:
    直接在package.json添加:
    "resolutions": {
      "strip-ansi": "^6.0.1"
    }
    
    删除node_modules和yarn.lock后执行yarn install

2. 升级ESLint到兼容版本

旧版ESLint对ES模块的支持不完善,升级到最新稳定版即可兼容:

npm install eslint@latest --save-dev
# 或yarn命令
yarn add eslint@latest --dev

升级完成后重新运行ESLint验证问题是否解决。

3. 更换ESLint formatter

若不想调整依赖版本,可临时切换到其他兼容的formatter,比如simple或unix:

  • 命令行指定:eslint . --format simple
  • 配置文件指定(以.eslintrc.js为例):
    module.exports = {
      // 其他配置项
      formatter: 'simple'
    }
    

内容的提问来源于stack exchange,提问作者nayaCirdan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 07:52:09