5

Im new to typescript. Im using a serverless solution from google (google cloud functions) that in the background is running Node.js with typescript. When I get an "runtime error" in the logs I see an error in a .js file, this makes sense since .ts code is compiled to .js but it makes debugging a lot harder as I write typescript code and not javascript code. In general I would like to see the line that produced the error in .ts and not in .js. Is this possible?

2
  • You can have TypeScript create source maps, which can be used to relate the line and column informtaion back to the original TypeScript source. Debuggers can use them automatically, no idea how to integrate them into the logs you're dealing with, though. Commented Jul 6, 2019 at 13:08
  • Does this answer your question? How to get error information relating to TypeScript file lines number in node.js? Commented Nov 2, 2020 at 14:49

1 Answer 1

7

You will need a "source map" to map the JavaScript line numbers to TypeScript line numbers.

You can do this with the source-map-support node module. First install it in your project:

npm install source-map-support

Then add this to your TypeScript file (index.ts):

require('source-map-support').install()

Then, if you function crashes, the line numbers should show TypeScript source lines.

Sign up to request clarification or add additional context in comments.

1 Comment

That's really cool. (I kind of missed the fact it was Node.js running the function in the background, but...I didn't know that anyway, so. :-) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.