安装@types/lodash后Node.js TypeScript项目编译错误求助
解决lodash与@types/lodash版本兼容导致的TypeScript编译错误
一、用匹配版本组合修复
lodash 4.x核心包完全适配同大版本的@types/lodash,你当前用的@types/lodash@^4.14.195版本太老,和新版TypeScript存在解析冲突。直接安装匹配的版本对:
npm uninstall lodash @types/lodash npm install lodash@^4.17.21 @types/lodash@^4.17.21
二、检查TypeScript配置
如果版本更新后仍报错,确认tsconfig.json的关键配置:
- 确保
moduleResolution适配Node.js模块规范,同时开启ES模块兼容选项:
{ "compilerOptions": { "moduleResolution": "node16", "target": "ES2020", "esModuleInterop": true, "allowSyntheticDefaultImports": true } }
三、调整导入方式(可选)
配合上面的配置,可将导入语句改为默认导入,避免模块解析异常:
// 替换原import语句 import _ from 'lodash';
四、清理依赖缓存
若以上步骤无效,清理依赖缓存后重新安装:
rm -rf node_modules package-lock.json npm install npx tsc --clean
内容的提问来源于stack exchange,提问作者porton
相关产品推荐
相关产品推荐

