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

Node.js+TypeScript中Error堆栈无法识别CallSite类型问题排查

问题描述

我编写了如下代码:

Error.prepareStackTrace = function (
  error: Error,
  stack: Array<CallSite>,
) {
  return self.parse(fiber, error, stack)
}

尝试从@types/node中引入CallSite类型,但无法找到该类型。以下是我的配置信息:

tsconfig.json

{
  "compilerOptions": {
    "module": "ES6",
    "target": "ES6",
    "lib": ["es2020"],
    "outDir": "host",
    "rootDir": "make",
    "sourceMap": true,
    "moduleResolution": "node",
    "strictNullChecks": true,
    "strict": true,
    "esModuleInterop": true,
    "noUncheckedIndexedAccess": true,
    "baseUrl": "./make",
    "paths": {
      "~": ["."]
    },
    "types": ["node"],
    "typeRoots": ["node_modules/@types"],
    "noImplicitAny": true
  },
  "include": ["make"],
  "exclude": ["node_modules"]
}

package.json

{
  "type": "module",
  "scripts": {
    "build": "tsc && tsc-alias",
    "watch": "concurrently --kill-others \"tsc -w\" \"tsc-alias -w\"",
    "lint": "eslint --ext .ts ./make",
    "lint:fix": "npm run lint -- --fix",
    "test": "node --no-warnings --loader @esbuild-kit/esm-loader host/build"
  },
  "devDependencies": {
    ...
    "@types/node": "^18.11.17",
    "ts-node": "^10.9.1",
    "tsc-alias": "^1.8.2",
    "typescript": "^4.9.4"
  }
}

原因分析

CallSite类型在@types/node中并非全局暴露的,它是定义在NodeJS命名空间下的内部接口,直接写CallSite会导致TypeScript无法识别该类型。

解决方法

你的配置已经通过types: ["node"]引入了@types/node,NodeJS命名空间会自动被TypeScript识别,直接通过NodeJS.CallSite引用类型即可:

Error.prepareStackTrace = function (
  error: Error,
  stack: Array<NodeJS.CallSite>,
) {
  return self.parse(fiber, error, stack)
}

如果觉得写法繁琐,也可以先定义类型别名简化:

type CallSite = NodeJS.CallSite;

Error.prepareStackTrace = function (
  error: Error,
  stack: Array<CallSite>,
) {
  return self.parse(fiber, error, stack)
}

若仍然报错,可重新安装对应版本的@types/node确保依赖完整:

npm install @types/node@18.11.17 --save-dev

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 17:15:11