Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 669
k0pernikus Avatar asked Feb 07 '17 10:02

k0pernikus


People also ask

Does Nodemon work with TypeScript?

As of v1. 19.0, nodemon has inbuilt support for TypeScript files with help from ts-node that requires no manual configuration. By default, nodemon uses the node CLI as an execution program for running JavaScript files; for TypeScript files, nodemon uses ts-node as the execution program instead.

What is source map in TypeScript?

Explanation: TypeScript Map files are source map files that let tools map between the emitted JavaScript code and the TypeScript source files that created it. And these Source Map files will then help to debug the Typescript file instead of debugging the emitted JavaScript file.

CAN node run TypeScript files?

We can use the ts-node package to execute TypeScript files from the command line. Install it with npm or other package manager. After that, simply execute the TypeScript files with the command: ts-node filename.


2 Answers

🚩 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

like image 159
Stephen Paul Avatar answered Sep 22 '22 04:09

Stephen Paul


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/ 
like image 28
k0pernikus Avatar answered Sep 23 '22 04:09

k0pernikus