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

TypeScript项目构建报错:@types/node与react-native中require重复定义

解决TypeScript项目中require重复标识符错误

这个错误是因为@types/node和@types/react-native两个类型定义文件都声明了require标识符,导致TypeScript编译时冲突。虽然你已经在tsconfig.json里排除了node_modules,但TypeScript在处理类型定义时仍会扫描@types目录下的文件,所以这个排除规则不会生效在类型文件上。下面是几个可行的解决办法:

方法一:跳过库类型检查(最简单快速)

在tsconfig.json的compilerOptions中添加"skipLibCheck": true,这样TypeScript会跳过对所有第三方库类型文件的检查,直接避免这类冲突:

{
  "compilerOptions": {
    "target": "es2015",
    "module": "es2015",
    "moduleResolution": "node",
    "jsx": "react",
    "outDir": "artifacts",
    "rootDir": "src",
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": false,
    "preserveConstEnums": true,
    "allowJs": false,
    "sourceMap": true,
    "noImplicitReturns": true,
    "noUnusedParameters": true,
    "noUnusedLocals": true,
    "skipLibCheck": true // 新增这一行
  },
  "filesGlob": [
    "src/**/*.ts",
    "src/**/*.tsx"
  ],
  "types": [
    "react",
    "react-native",
    "jest"
  ],
  "exclude": [
    "android",
    "ios",
    "build",
    "node_modules"
  ],
  "compileOnSave": false
}

方法二:明确指定要引入的类型

调整tsconfig.json的compilerOptions,通过typeRoots只引入你需要的类型包,避免自动引入@types/node:

{
  "compilerOptions": {
    // 你的其他配置...
    "types": ["react", "react-native", "jest"],
    "typeRoots": [
      "./node_modules/@types/react",
      "./node_modules/@types/react-native",
      "./node_modules/@types/jest"
    ]
  },
  // 其他配置...
}

方法三:移除不必要的@types/node依赖

如果你的项目不需要@types/node的类型定义,可以直接卸载它:

yarn remove @types/node --dev

不过要注意,有些第三方依赖可能会间接依赖@types/node,所以这个方法不一定总是可行,此时方法一或二会更稳妥。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:33:28