如何在 Nest.js 中参考打字稿源打印堆栈跟踪

How to print stack trace with reference to typescript source in Nest.js

我正在开发 Nest.js 服务器并希望能够在控制台中打印有用的堆栈跟踪(例如 console.log)。默认情况下,它 returns 对编译源 (.js) 中行号的引用。这对调试没有用,因为它缺少对原始源文件 (.ts) 中行号的引用

这是我的tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "_baseUrl": "./",
    "incremental": true
  },
  "exclude": ["node_modules", "dist"]
}

.map 文件也在 dist 文件夹中生成,尽管在控制台中检查堆栈跟踪时似乎没有用。

出于可见性目的:添加 source-map-support NPM 包允许在堆栈跟踪中跟踪打字稿文件。

可以使用node -r source-map-support/register fileToRun.js在命令行上添加,也可以使用

以编程方式添加
import * as sourceMapSupport from 'source-map-support';
sourceMapSupport.install();

import { install } from 'source-map-support';
install();

或使用 ES6 模块

import 'source-map-support/register';