Next.js项目更新依赖后ESLint格式化器加载失败求助
报错信息
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用户:
- 安装依赖工具:
npm install -D npm-force-resolutions - 在
package.json中添加配置:"resolutions": { "strip-ansi": "^6.0.1" }, "scripts": { "preinstall": "npx npm-force-resolutions" } - 删除
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

