Is there source map support for typescript in node / nodemon?

前端 未结 5 2104
盖世英雄少女心
盖世英雄少女心 2021-01-31 14:34

I have a node project written in typescript@2.

My tsconfig has sourceMap set to true and the *.map.js files are generated. When I

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-31 15:21

    Install source map support:

    npm install source-map-support
    

    (I run in in production as well, as it immensely helps finding bugs from the logs of when an error an occurs. I did not experience a large performance impact, yet your experience may be different.)

    Add to your tsconfig.json:

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

    When running your JavaScript file, add the require parameter:

    nodemon -r source-map-support/register dist/pathToJson.js
    
    node -r source-map-support/register dist/pathToJson.js
    

    Alternatively, you add in your entry call:

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

    yet I find this tedious is projects with multiple entry points.


    Sidenote: mocha also supports the --require / -r option, so to have the sourcemap support in mocha you can also call your tests with it, e.g. similar to:

    NODE_ENV=test npx mocha --forbid-only --require source-map-support/register --exit --recursive ./path/to/your/tests/
    

提交回复
热议问题