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

long包导致tsc构建失败,如何强制TypeScript不使用其UMD版本?

问题

Yarn安装其他模块时,会自动安装其依赖的long包的UMD版本(属于CommonJS模块)。执行tsc构建时出现以下错误:

node_modules/long/umd/index.d.ts:1:18 - error TS1479: The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("../index.js")' call instead.        

import Long from "../index.js";

无法直接修改该依赖文件。已尝试显式安装非UMD版本的long@4.0.0,但依赖long的其他模块仍会在自身node_modules目录中安装报错的版本;也尝试过使用可正常运行的顶层依赖版本,问题依旧。请问是否有办法强制TypeScript编译器不使用long的UMD版本?

相关配置文件

package.json

{
  "name": "<project>",
  "version": "1.0.0",
  "engines": {
    "node": "20.x.x"
  },
  "main": "index.js",
  "repository": "<repo>",
  "author": "<author>",
  "license": "MIT",
  "scripts": {
    "dev": "nodemon",
    "build": "tsc && tsc-alias",
    ...
  },
  "devDependencies": {
    "nodemon": "^3.0.1",
    "ts-node": "^10.9.1",
    "tsc-alias": "^1.8.7",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.3.0-dev.20230920"
  },
  "dependencies": {
    ...
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "module": "NodeNext",
    "lib": ["ESNext", "DOM", "DOM.Iterable"],
    "baseUrl": "src",
    "outDir": "./build",
    "sourceMap": true,
    "noImplicitAny": true,
    "types": ["./elevenlabs-api.d.ts"],
    "paths": {
      "@/*": ["./*"],
      ...
    }
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*.spec.ts"]
}
解决方案

1. 强制统一long版本

在package.json中添加resolutions字段,强制所有依赖树中的long都使用指定的非UMD版本(比如4.0.0):

{
  ...
  "resolutions": {
    "long": "4.0.0"
  }
}

添加后执行yarn install,Yarn会替换所有子依赖中的long版本,避免出现不同版本冲突。

2. 让TypeScript跳过UMD目录

修改tsconfig.json的exclude字段,添加long的UMD路径,让编译器忽略该目录下的文件:

{
  "exclude": [
    "node_modules",
    "**/*.spec.ts",
    "node_modules/long/umd/**/*"
  ]
}

建议结合上面的版本统一方案使用,确保TypeScript能找到正确的类型定义文件。

3. 调整TypeScript模块兼容配置

在tsconfig.json的compilerOptions中添加模块兼容相关配置,优化CommonJS与ES模块的混合解析:

{
  "compilerOptions": {
    ...
    "moduleResolution": "NodeNext",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}

这两个配置能让TypeScript更好地处理不同模块规范的导入逻辑,减少类型不兼容报错。

4. 临时禁用指定错误

如果以上方法都无法解决,可临时在tsconfig.json中禁用TS1479错误:

{
  "compilerOptions": {
    ...
    "disableErrorChecking": false,
    // 禁用TS1479错误
    "errors": {
      "TS1479": "off"
    }
  }
}

注意:这只是临时 workaround,不建议长期使用,优先通过前三种方法从根源解决问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 05:13:38