Node.js + TypeScript项目运行报TSError编译错误,求排查解决
Node.js + TypeScript项目TSError编译错误排查与解决
错误信息
C:\Users\Projects\api\node_modules\ts-node\src\index.ts:307 throw new TSError(formatDiagnostics(diagnosticList, cwd, ts, lineOffset)) ^ TSError: ⨯ Unable to compile TypeScript src\app.ts (12,13): This expression is not callable. Type 'typeof e' has no call signatures. (2349) src\app.ts (24,22): Property 'status' does not exist on type 'Response<any, Record<string, any>>'. (2339) src\app.ts (32,13): Property 'json' does not exist on type 'Response<any, Record<string, any>>'. (2339) at getOutput (C:\Users\mysystem\Projects\myProject\node_modules\ts-node\src\index.ts:307:15) at C:\Users\mysystem\Projects\myProject\node_modules\ts-node\src\index.ts:336:16 at Object.compile (C:\Users\mysystem\Projects\myProject\node_modules\ts-node\src\index.ts:498:11) at Module.m._compile (C:\Users\mysystem\Projects\myProject\node_modules\ts-node\src\index.ts:392:43) at Module._extensions..js (node:internal/modules/cjs/loader:1149:10) at Object.require.extensions.<computed> [as .ts] (C:\Users\mysystem\Projects\myProject\node_modules\ts-node\src\index.ts:395:12) at Module.load (node:internal/modules/cjs/loader:975:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Module.require (node:internal/modules/cjs/loader:999:19) at require (node:internal/modules/cjs/helpers:102:18)
环境配置
- node: 17.3.0
- typescript: 4.6.4
- yarn: v1.22.15
tsconfig.json
{ "compilerOptions": { "lib": [ "es5", "es6" ], "target": "es5", "module": "commonjs", "moduleResolution": "node", "outDir": "./build", "emitDecoratorMetadata": true, "experimentalDecorators": true, "esModuleInterop": true, "allowSyntheticDefaultImports": true, "sourceMap": true } }
app.ts代码
import * as express from "express"; import * as bodyParser from "body-parser"; import { Request, Response } from "express"; import * as morgan from "morgan"; import { Routes } from "./routes"; import { validationResult } from "express-validator"; function handleError(err, _req, res, _next) { res.status(err.statusCode || 500).send(err.message); } const app = express(); app.use(morgan("tiny")); app.use(bodyParser.json()); Routes.forEach((route) => { (app as any)[route.method]( route.route, ...route.validation, async (req: Request, res: Response, next: Function) => { try { const errors = validationResult(req); if (!errors.isEmpty()) { return res.status(400).json({ errors: errors.array() }); } const result = await new (route.controller as any)()[route.action]( req, res, next ); res.json(result); } catch (err) { next(err); } } ); }); app.use(handleError); export default app;
已尝试操作
- 重装ts-node
- 清理项目缓存
解决方案建议
1. 修正类型引用冲突
错误核心是Response类型被错误识别为全局DOM环境的Response(而非Express框架的Response),导致找不到status、json方法。
修改handleError函数,明确指定参数类型为Express的Request和Response:
function handleError(err: any, _req: Request, res: Response, _next: Function) { res.status(err.statusCode || 500).send(err.message); }
2. 排查ts-node版本冲突
确保项目本地的ts-node版本与其他正常运行的机器一致,避免全局安装的ts-node干扰。运行以下命令查看本地版本:
yarn list ts-node
运行项目时强制使用本地ts-node:
yarn ts-node src/app.ts
3. 清理TypeScript相关缓存
删除项目根目录下的.ts-node文件夹和tsconfig.tsbuildinfo文件(如果存在),彻底清理编译缓存。
4. 检查全局TypeScript版本
避免全局安装的TypeScript与项目本地版本冲突,执行以下命令对比版本:
# 查看全局版本 tsc -v # 查看项目本地版本 yarn tsc -v
若版本不一致,卸载全局TypeScript:
npm uninstall -g typescript
内容的提问来源于stack exchange,提问作者rishu rathaur
相关产品推荐
相关产品推荐

