How to Introduce node-source-map-support to Display .ts File Locations in Stack Traces in NestJS
I’ll show you how to introduce node-source-map-support to display .ts file locations in stack traces in NestJS.
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.
npm install source-map-support --save
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 {}
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.