75

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.

7 Answers 7

44

🚩 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

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

5 Comments

inlineSourceMap and sourceMap compiler options are mutually exclusive
If you add the cli usage to your answer (as I pointed out in my own), I will accept yours and delete my own. ( stackoverflow.com/a/58455205/457268 )
@k0pernikus thanks. Apologies, not 100% sure I understand. Feel free to edit to my answer to show what you mean.
Any way to do this natively without bloating a package with source-map-support?
From node v12, you can use the flag --enable-source-maps
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

3 Comments

NODE_OPTIONS=--enable-source-maps npm test is the command I was looking for
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.
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.
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/

Comments

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.

Comments

10

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

Comments

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)

2 Comments

Any downsides to using this in production? (server side)
@Sev Not as far as we know (we do use it in production)
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

1 Comment

Looks like that with such settings nyc doesn't report coverage

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.