debugging a TypeScript script with NodeJS

I am trying to run a TypeScript script in NodeJS in debug mode. I have the NodeJS plugin. I can't get it to work. I used this page for reference, the section headed 'Debugging TypeScript': 

https://www.jetbrains.com/help/idea/running-and-debugging-typescript.html

The TypeScript file is compiling to JavaScript, with a .js file and a .map file generated. I created a Node.js run configuration. It works if I point it directly at the generated .js file, but not if I point it at the .ts file. If I do that, I get a syntax error, as if Intellij is trying to run the .ts file as JavaScript.

I tried adding a tsconfig.json file with sourceMap=true, but that didn't seem to help. Alternatively, in Languages & Frameworks --> TypeScript I tried checking the 'Also for projects without tsconfig.json' checkbox and I added --sourceMap=true in the Options field. That didn't help either.

Using Intellij 2019, System node.exe in Windows, Bundled TypeScript.

If I point to the .js file and add breakpoints in there, I am able to debug that way, but it would be nice to debug directly in TypeScript.

0
4 comments

>It works if I point it directly at the generated .js file, but not if I point it at the .ts file. If I do that, I get a syntax error, as if Intellij is trying to run the .ts file as JavaScript.

 

this is expected - Node.js can't execute Typescript directly, it has to be compiled to javascript to make this work. So, you have to either precompile your .ts files (using the built-in compiler, for example) and specify the generated .js file in your run configuration, or compile it on-the-fly by adding --require ts-node/register to Node parameters:

But, regardless of the way you compile your code, you should still be able to set breakpoints in .ts file and debug them (of course, if the source maps are there)

4

Is there not a way to set up a Macro or something so when you right click on the file (like a JS file) you are able to run/debug the cross-compiled JS file?  

0

A heads-up for anyone setting up with ts-node with the instructions here (https://www.jetbrains.com/help/webstorm/running-and-debugging-typescript.html#ws_ts_run_debug_server_side_ts_node) in a lerna project. I faced some TS compile errors, Setting the working directory to the sub-package dir fixed this for me.

0

Please sign in to leave a comment.