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

读取dist目录JS文件时遇TS 2304/2580错误,求解决方案

解决「Cannot find name 'require'/'exports'/'process'」TS错误的可行方案

问题背景

读取dist目录下的.js文件时,持续触发以下TypeScript错误:

  • Cannot find name 'require' ts 2304
  • Cannot find name 'exports' ts 2304
  • Cannot find name 'process' ts 2580

已尝试npm install @types/node --save-dev、重启VSCode、重建node_modules与yarn.lock后重新安装依赖,但问题未解决。

配置信息

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["ES2015"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "module": "CommonJS",
    "rootDir": "src",
    "moduleResolution": "node",
    "typeRoots": ["./node_modules/@types"],
    "types": ["node"],
    "resolveJsonModule": true,
    "allowJs": true,
    "checkJs": true,
    "declaration": true,
    "declarationMap": true,
    "sourceMap": true,
    "outDir": "dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  },
  "compileOnSave": true,
  "exclude": ["node_modules"],
  "include": ["src/**/*.ts"]
}

package.json

{
  "name": "api",
  "version": "0.0.1",
  "description": "",
  "main": "./dist/index.js",
  "dependencies": {
    "@mikro-orm/core": "^5.3.1",
    "@mikro-orm/postgresql": "^5.3.1",
    "class-validator": "^0.13.2",
    "dotenv": "^16.0.1",
    "esm": "^3.2.25",
    "graphql": "^16.6.0",
    "reflect-metadata": "^0.1.13",
    "type-graphql": "^1.1.1",
    "typedi": "^0.10.0",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "@mikro-orm/cli": "^5.3.1",
    "@types/node": "^18.7.11",
    "@types/uuid": "^8.3.4",
    "ts-node": "^10.9.1"
  },
  "mikro-orm": {
    "useTsNode": true,
    "configPaths": [
      "./src/mikro-orm.config.ts",
      "./dist/mikro-orm.config.js"
    ]
  }
}

依赖版本

├── @mikro-orm/cli@5.3.1
├── @mikro-orm/core@5.3.1
├── @mikro-orm/postgresql@5.3.1
├── @types/node@18.7.11
├── @types/uuid@8.3.4
├── class-validator@0.13.2
├── dotenv@16.0.1
├── esm@3.2.25
├── graphql@16.6.0
├── reflect-metadata@0.1.13
├── ts-node@10.9.1
├── type-graphql@1.1.1
├── typedi@0.10.0
└── uuid@8.3.2

可行解决方法

1. 将dist目录加入tsconfig.json的排除列表

当前tsconfig.json仅排除了node_modules,但dist是编译输出目录,开启checkJs: true后TypeScript会自动检查该目录下的JS文件,导致CommonJS语法无法被正确识别。修改exclude字段:

"exclude": ["node_modules", "dist"]

2. 关闭JS文件检查(推荐)

项目源码全部为TypeScript文件,无需检查编译后的JS文件。将checkJs设为false:

"checkJs": false

若需检查项目内其他非dist目录的JS文件,可保留checkJs: true,配合第一步的exclude: ["dist"]即可。

3. 确保VSCode使用项目本地TypeScript版本

VSCode默认可能使用全局TypeScript版本,导致项目的@types/node配置无法生效:

  • 点击VSCode右下角显示的TypeScript版本号(如4.9.3)
  • 选择「Use Workspace Version」(即项目node_modules中的TypeScript版本)
  • 若未显示版本选项,可在VSCode设置中添加:
    "typescript.tsdk": "./node_modules/typescript/lib"
    

4. 排查多tsconfig文件冲突

若项目中存在多个tsconfig.json(如子目录下的配置文件),VSCode可能加载错误的配置。确保根目录tsconfig.json是唯一有效配置,或通过extends字段统一管理子配置。

5. 手动补充环境类型声明(兜底方案)

若以上方法无效,可在src目录下创建env.d.ts文件,手动声明Node.js环境变量:

declare var require: NodeRequire;
declare var exports: typeof module.exports;
declare var process: NodeJS.Process;

该文件会被TypeScript自动识别,补充缺失的类型定义。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 17:54:32