VS Code创建新文件时ESLint报错(Turborepo+React-i18next环境)
解决Turborepo + Next.js + react-i18next的ESLint路径错误问题
实用修复方案
1. 给ESLint配置pnpm依赖解析器
在项目根目录的.eslintrc.js(或.eslintrc.json)中添加pnpm专属的解析配置,让ESLint适配pnpm的扁平依赖结构:
module.exports = { settings: { 'import/resolver': { node: { paths: ['src'], extensions: ['.js', '.jsx', '.ts', '.tsx'], }, pnpm: {}, // 启用pnpm解析逻辑 }, }, };
如果还没安装对应依赖,先执行:
pnpm add -D eslint-import-resolver-pnpm
2. 修正TypeScript的类型文件路径配置
在根目录或Next.js应用的tsconfig.json里,明确指定类型文件的根路径,避免ESLint误判路径:
{ "compilerOptions": { "typeRoots": ["./node_modules/@types", "./node_modules/react-i18next/dist"] } }
3. 关闭ESLint自动重启,手动刷新服务
打开VS Code设置,搜索ESLint: Auto Restart并关闭。之后每次新建文件,不用重启VS Code,直接按Ctrl+Shift+P(Windows/Linux)或Cmd+Shift+P(Mac),执行ESLint: Restart ESLint Server即可清除错误。
4. 彻底清理缓存后重装依赖
执行以下命令清理Turborepo和pnpm的缓存,重新安装依赖:
turbo clean pnpm store prune pnpm install
5. 给Next.js添加webpack模块解析配置
在Next.js应用的next.config.js中补充webpack配置,强制指定模块查找路径:
const path = require('path'); const nextConfig = { webpack: (config) => { config.resolve.modules.push(path.resolve('./node_modules')); return config; }, }; module.exports = nextConfig;
内容的提问来源于stack exchange,提问作者Alesij
相关产品推荐
相关产品推荐

