How to Introduce node-source-map-support to Display .ts File Locations in Stack Traces in NestJS

Tadashi Shigeoka ·  Sat, April 3, 2021

I’ll show you how to introduce node-source-map-support to display .ts file locations in stack traces in NestJS.

NestJS

Background: Want NestJS Stack Traces to Show .ts File Locations

I was having trouble identifying error locations because NestJS stack traces were showing .js file locations, so I introduced source maps to change this to .ts files.

Solution: Introduce node-source-map-support

Installing source-map-support

npm install source-map-support --save

import 'source-map-support/register';

Simply adding import 'source-map-support/register'; to your AppModule is all you need.

import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
if (process.env.NODE_ENV === 'development') {
  require('source-map-support/register');
}

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

Side Note: Source Maps Don't Reflect Properly to Sentry

The reason I only use require('source-map-support/register'); when NODE_ENV is ‘development’ in the code above is that using ‘source-map-support/register’ in ‘production’ prevents Source Maps from reflecting properly to Sentry.

if (process.env.NODE_ENV === 'development') {
  require('source-map-support/register');
}

That’s all from the Gemba.