5

I am trying to get sourcemaps to work, when developing firebase functions in typescript.

In my tsconfig.json file a have source maps enabled.

This generated the sourcemaps. I then include this line in my index.ts file:

import 'source-map-support/register';

And then it seems to work. Is this correct configured, and source-map-support to the projects package.json file?

DauleDK
  • 3,313
  • 11
  • 55
  • 98

1 Answers1

9

Yes, you need to do a few things, some of which is documented here:

  1. npm install source-map-support

  2. Enable sourceMap in tsconfig.json (not package.json!) by adding:

  "compilerOptions": {
    "sourceMap": true,
    ...
  },
  1. Import the library into your file
    • If you're using ES6 modules: import 'source-map-support/register'
    • If you're using commonJS modules: require('source-map-support').install();

the result would transform this:

TypeError: Cannot read property 'current_location' of null
    at /user_code/lib/http_actions.js:173:74
    at next (native)
    at fulfilled (/user_code/lib/http_actions.js:4:58)
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)

into this:

TypeError: Cannot read property 'current_location' of null
    at /user_code/src/http_actions.ts:183:33
    at next (native)
    at fulfilled (/user_code/lib/http_actions.js:4:58)
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)
Ben Butterworth
  • 22,056
  • 10
  • 114
  • 167
Benos
  • 676
  • 7
  • 17
  • Does this still work? It seems like it doesn't work anymore – Alex Gogl Jul 08 '20 at 07:01
  • I can confirm that it does indeed still work exactly as specified! As per [this doc](https://firebase.google.com/docs/functions/reporting-errors), as long as the `logger.error` call includes an `Error` object, we get the stack printout. And the filename and line number points correctly to the .ts source file. – AverageHelper Sep 13 '20 at 02:34
  • I went into some detail on my implementation [here](https://stackoverflow.com/a/63866829). – AverageHelper Sep 13 '20 at 03:20