Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I am developing a Nest.js server and would like to be able to print useful stack trace in console (e.g. console.log). By default, it returns a reference to the line number in the compiled sources (.js). This is not useful for debugging as it's missing the reference to the line number in the original source files (.ts)

Here is my 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"] } 

The .map files are generated in the dist folder as well, though it seems to be of no use when checking stack traces in the console.

like image 721
Alex Predescu Avatar asked Nov 22 '19 19:11

Alex Predescu


1 Answers

For visibility purposes: adding the source-map-support NPM package allows for tracing typescript files in the stack trace.

It can be added on the command line with node -r source-map-support/register fileToRun.js or programatically with

import * as sourceMapSupport from 'source-map-support'; sourceMapSupport.install(); 

OR

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

OR with ES6 modules

import 'source-map-support/register'; 
like image 149
Jay McDoniel Avatar answered Sep 21 '22 19:09

Jay McDoniel