how to get error of typescript line no in nodejs

I am using typescript for my nodejs backend development. whenever I get an error in nodejs it shows me the line no in js (compiled ts code).

(if you have used ionic) we get the error corresponding to the ts file.

Is there any way I get error line no if my ts file. If yes please explain how and what changes I need to do to the config file. thanks in advance.

2 answers

  • answered 2018-05-16 06:20 Manish Kumawat

    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

  • answered 2018-05-16 06:20 Ritwick Dey

    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