22

I am using TypeScript for my node.js backend development. Whenever I get an error in node.js it shows me the line number relating to the transpiled JavaScript (.js) files, not the TypeScript (.ts) files.

If you have used ionic, we get the error corresponding to the typescript file there.

Is there any way I get error line number relating to my typescript file? If yes please explain how and what changes I need to do to the config file.

1

3 Answers 3

22

Install source-map-support package.

$ npm install --save-dev source-map-support

Add this line in the entry point of node.js

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

In your tsconfig.json,

{
   "compilerOptions": {
      "sourceMap": true
   }
}

Ref: https://github.com/evanw/node-source-map-support#typescript-demo

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

5 Comments

I don't see any source map file. my config file is { "compilerOptions": { "sourceMap": true, } }, "include": [ "src/**/*" ] }
How you're compiling ? It is tsc -p tsconfig.json
Is it possible to register source map support without calling require('source-map-support').install()?
with ES6 use import 'source-map-support/register'
4

It's also possible to register source-map via node's require option require within the cli:

node -r source-map-support/register compiled.js

The docs also point out that babel-register already includes source-map-support and that one can use it with mocha this way:

mocha --require source-map-support/register tests/

I did not want to call

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

on each entry file. (It may be sufficient if you have only one entrypoint. Yet I have many.)

The basic setup still is the same as outlined by the existing solution.

Comments

-1

You can achieve taht by having map files. When you have map files and map files are associated with js files then it will automatically give you line number from TS file.

You can generate map files using some configuration in tsconfig.json file

{
  "compilerOptions": {
    "sourceMap": true,
    }
}

You need to keep sourceMap setting as true

5 Comments

I don't see any source map file. my config file is { "compilerOptions": { "sourceMap": true, } }, "include": [ "src/**/*" ] }
Can you please paste complete tsconfig.json file?
What's the "it" in the "it will automatically give you line number from TS file"? The point of the question is that the OP wants node to show the line number of the error in the .ts file, not in the generated .js file.
Here "it" means node. If you generate a map file for a ts file, then you will have ability to directly debug ts file. Which means you can get the exact place of error.
I’ve used this but only for vscode debug support. trace or stack I don’t know if that works accurately.

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.