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

7

74

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 execute my transpiled *.js JavaScript files via node or nodemon, I only see the error messages relative to the js file and not to the mapped typescript files; I assume it's completely ignored.

Is sourceMap support only intended for browser-support? Or can I use it together with node or nodemon? If the latter, how would I enable it?

I want to see runtime errors detected from an executed javascript file relative to the original typescript file.

Lancelle answered 7/2, 2017 at 10:56 Comment(0)
W
33

The answers here are correct for Node versions before v12.12.0, which added the --enable-source-maps flag (experimental until v15.11.0, v14.18.0). With that enabled, source maps are applied to stack traces without an additional dependency. As demonstrated in this article, it has the slightly different and possibly beneficial behavior of including both the generated .js file location and the source file location. For example:

Error: not found
    at Object.<anonymous> (/Users/bencoe/oss/source-map-testing/test.js:29:7)
        -> /Users/bencoe/oss/source-map-testing/test.ts:13:7
Wellspoken answered 6/11, 2020 at 20:25 Comment(3)
NODE_OPTIONS=--enable-source-maps npm test is the command I was looking for – Flossi
I found that this didn't work unless I put --enable-source-maps BEFORE the path to the script file. Perhaps this is normal for Node command line switches; I'm not sure. – Leeuwenhoek
Note: There are complaints of --enable-source-maps slowing down requests (See github.com/nodejs/node/issues/41541). Maybe a little bit of polishing is required before we can use this in production. – Lyon
R
43

🚩 for Node versions since v12.12, there is an easier and better solution.

I recently got this working in my express app. Steps as follows:

Install the required library:

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

In your entry point (eg app.ts):

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

In your app.ts, you may also require better logging for errors within promises:

process.on('unhandledRejection', console.log);

In your tsconfig, under compilerOptions:

"inlineSourceMap": true

Roncesvalles answered 16/5, 2017 at 7:22 Comment(5)
inlineSourceMap and sourceMap compiler options are mutually exclusive – Blinnie
If you add the cli usage to your answer (as I pointed out in my own), I will accept yours and delete my own. ( https://mcmap.net/q/270008/-is-there-source-map-support-for-typescript-in-node-nodemon ) – Lancelle
@Lancelle thanks. Apologies, not 100% sure I understand. Feel free to edit to my answer to show what you mean. – Roncesvalles
Any way to do this natively without bloating a package with source-map-support? – Voltz
From node v12, you can use the flag --enable-source-maps – Porfirioporgy
W
33

The answers here are correct for Node versions before v12.12.0, which added the --enable-source-maps flag (experimental until v15.11.0, v14.18.0). With that enabled, source maps are applied to stack traces without an additional dependency. As demonstrated in this article, it has the slightly different and possibly beneficial behavior of including both the generated .js file location and the source file location. For example:

Error: not found
    at Object.<anonymous> (/Users/bencoe/oss/source-map-testing/test.js:29:7)
        -> /Users/bencoe/oss/source-map-testing/test.ts:13:7
Wellspoken answered 6/11, 2020 at 20:25 Comment(3)
NODE_OPTIONS=--enable-source-maps npm test is the command I was looking for – Flossi
I found that this didn't work unless I put --enable-source-maps BEFORE the path to the script file. Perhaps this is normal for Node command line switches; I'm not sure. – Leeuwenhoek
Note: There are complaints of --enable-source-maps slowing down requests (See github.com/nodejs/node/issues/41541). Maybe a little bit of polishing is required before we can use this in production. – Lyon
L
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/
Lancelle answered 18/10, 2019 at 16:56 Comment(0)
S
18

I found this npm module which seems to do the trick:

https://github.com/evanw/node-source-map-support

run npm install source-map-support --save at the root of your node project and add import 'source-map-support/register' to your main.ts or index.ts file.

That's it.

Suborbital answered 15/2, 2017 at 10:12 Comment(0)
I
9

For Node versions from v12.12.0 use the --enable-source-maps flag when you run node.

Example: node --enable-source-maps main.js

Do not install "source-map-support" for Node versions from v12.12.0

Indentation answered 4/11, 2021 at 23:8 Comment(0)
I
6

Source map support works perfectly fine with node

All you need to do is add

"source-map-support": "0.4.11",

to dependencies or dev-dependencies in package.json by running

npm install --save source-map-support

And in your entry point ts file, simply add at the top

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

(note: this is calling nodeJS require - there is no need for source-map-support definition files)

Initial answered 15/2, 2017 at 10:22 Comment(2)
Any downsides to using this in production? (server side) – Alon
@Alon Not as far as we know (we do use it in production) – Initial
P
1

If you are using node (16.20.2) nyc(15.1.0) and mocha(8.1.3) this worked for me.

The error stack pointed me to the line 4:

enter image description here

Solution

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

.nycrc.json

{
  "include": "src/main",  
  "all": true
}

package.json

  "scripts": {    
    "test": "NODE_OPTIONS=--enable-source-maps nyc --reporter=html --reporter=json-summary mocha 'src/test/node/**/*.test.js' --exit"
  }

The result error show the exact line 45 and extra information

enter image description here

Pajamas answered 7/1 at 20:48 Comment(1)
Looks like that with such settings nyc doesn't report coverage – Phthisis

© 2022 - 2024 β€” McMap. All rights reserved.