Sul Aga
Sul Aga

Reputation: 6732

How to debug server side TypeScript code in WebStorm

Comparing this to Visual Studio Code all you need to do is allow source maps and VSCode will debug TypeScript however I can't achieve the same on WebStorm.

I can easily debug server side JavaScript in WebStorm but not TypeScript

Upvotes: 14

Views: 10193

Answers (7)

Sidonai
Sidonai

Reputation: 3686

Create a package.json with a build and start scripts:

//package.json

{
...
"scripts": {
    "start": "npm run build && node ./dist/index.js",
    "build": "webpack --config ./webpack.config.js",
...
}

In this configuration, all compiled files are located under dist/. The entry point is index.js.

Make sure your tsconfig.json has the sourceMap option enabled:

//tsconfig.json

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

In your webpack.config.js file, make sure you have the following properties as shown below:

//webpack.config.js

module.exports = {
    mode: 'development', // This is important!, if you set 'production' it won't let you debug
    devtool: 'source-map',
    ...
}

Finally, set up your WebStorm Run/Debug Configuration as follows:

...
package.json: ~/path/to/your/package.json
Command: run
Scripts: start
...

Now add a breakpoint and start debugging

Upvotes: 0

Igor W.
Igor W.

Reputation: 431

There is not much needed to debug a Node.js project with TypeScript when using Webstorm/IntelliJ.

You should make sure you have enabled source maps in your tsconfig.json:

//tsconfig.json
{
  "compilerOptions": {
    ...,
    "sourceMap": true,
    ...
  }
}

Source maps will be needed to allow you to set breakpoints directly in your TypeScript files.

Then you should remember to use Node option --inspect or --inspect-brk. I would suggest in most cases to use inspect-brk since this one is pausing execution once Node is up and running.

You can add simple script to run your TypeScript Node app using eg. ts-node in a package.json:

//package.json
{
  "scripts": {
    "dev-debug": "ts-node --inspect-brk src/index.ts" 
  }
}

And run that using pnpm|yarn|npm package manager:

#shell
pnpm run dev-debug

Then in your shell you will get nice URL like ws://127.0.0.1:9229/0f2c936f-b1cd-4ac9-aab3-f63b0f33d55e which you can use to attach to your debugger.

To do that in WebStorm/IntelliJ you can just click on it with keys pressed:

⇧ + ⌘ + Click

Ctrl + Shift + Click

Keys depends on your OS.

After that you will have your debugger up and running in an IDE.

Upvotes: 2

Petey
Petey

Reputation: 3337

Just want to add what worked for me with Webstorm 2021.1.1

I found the easiest way is to go to your package.json and right click the green triangle next to the npm script you want to run. Then select debug.

I am able to apply the breakpoints to my typescript code and it works perfectly. Coming from .Net where it was always pretty straight forward to debug, I am glad to see webstorm making it just as simple.

This is my npm script that I choose to debug.

"dev": "env-cmd -f ./config/dev.env concurrently -k -n COMPILER,NODEMON -c gray,blue \"tsc -w\" \"nodemon -w dist dist/index.js\"",

Upvotes: 3

LeOn - Han Li
LeOn - Han Li

Reputation: 10194

was trying to find a way to let Webstorm/Intellij to watch the TS file change and restart server in debug mode. Looks like ts-node-dev which IHMO is faster than nodemon in terms of live-reload because it shares Typescript compilation process between restarts.

npm i ts-node-dev --save-dev

Then in your Run/Debug Configuration, add a node.js config with below params:

JavaScript file ---> node_modules/ts-node-dev/lib/bin.js

Applicationi parameters ---> --respawn -- src/script/local.server.ts

enter image description here

Now save the config and run with Debug, you should be able to set break point as well as live reload server on any TS code change.

I wrapped up a small library for this if you happen to develop with aws lambda

https://github.com/vcfvct/ts-lambda-local-dev

Upvotes: 8

Nicolas Zozol
Nicolas Zozol

Reputation: 7038

I'm using a specific version of node called ts-node.

Using ts-node with Webstorm

First add in your package.json file:

"devDependencies": {
    "ts-node": "8.1.0",
    "typescript": "3.2.4"
  },

Run npm install and the node_module/.bin/ directory will include the ts-node or ts-node.cmd required for Windows.

Obviously these versions will move. You may see inside the package.json of ts-node project which version of typescript they are using to be the closest as possible.

Then you can add breakpoints. The only downside I see is that you must define the Javascript file (which is a ts file) into the configuration, instead of just right-click + run.

If you have the xyz is not a function error, check that your tsconfig.json file doesn't have "noEmit": false,

Upvotes: 2

Pencroff
Pencroff

Reputation: 1009

For running WebStorm(2017.2.3) debugger around typescript sources I did:

  1. Setup Node.js configuration:
    • Working directory: root/of/the/project (where located my package.json)
    • JavaScript file: dist/index.js
  2. I am compiling my TypeScript with gulp-typescript, but more important the source-map files. So for compiling was used task like below:

    const gulp = require('gulp');
    const ts = require('gulp-typescript');
    const sourcemaps = require('gulp-sourcemaps');
    const merge = require('merge2');
    
    const tsProject = ts.createProject('tsconfig.json', {
      declaration: true,
      typescript: require('typescript'),
    });
    
    gulp.task('default', () => {
      const result = gulp.src('./app/**/*.ts')
        .pipe(sourcemaps.init())
        .pipe(sourcemaps.identityMap()) // optional
        .pipe(tsProject());
    
      return merge([
    result.js
          .pipe(sourcemaps.write('.', { includeContent: false, sourceRoot: '../app' }))
          .pipe(gulp.dest('dist')),
        result.dts
          .pipe(gulp.dest('dist')),
      ]);
    });
    

All source TS files located in './app' folder, all compiled files located in ./dist folder. Most important source-files option sourceRoot, wrong value not bring you to ts file.

By sourcemaps.write('.', { includeContent: false, sourceRoot: '../app' } I am writing my .map files beside .js files and make reference to app folder. I no need content in .map files because it's already there (app folder).

Thanks to @Ekaterina I was able to run Node debug with Typescript.

Upvotes: 1

jugglingcats
jugglingcats

Reputation: 749

For anyone else wrestling with debugging TypeScript in WebStorm/IDEA, I had similar frustrations as OP (possibly for different reason). My issue was simply that I didn't set the working directory to the dist folder in the node run configuration. I am running tests in Jest and assumed the working dir should be the root of my project. Set it to dist and debugging started working!

Further info...

Source .ts files in src

Typescript version: 2.0.3

File tsconfig.json:

{
  "compilerOptions": {
    "jsx": "react",
    "module": "commonjs",
    "noImplicitAny": false,
    "outDir": "dist",
    "preserveConstEnums": true,
    "removeComments": true,
    "sourceMap": true,
    "target": "es6",
    "moduleResolution": "node"
  },
  "exclude": [
    "node_modules",
    "dist"
  ]
}

Jest config (in package.json):

  "jest": {
    "scriptPreprocessor": "<rootDir>/node_modules/ts-jest/dist/preprocessor.js",
    "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx)$",
    "moduleFileExtensions": [
      "ts",
      "tsx",
      "js"
    ]
  }

Run configuration...

Working directory: <project_root>/dist

Javascript file: ../node_modules/jest-cli/bin/jest.js

Application params: --runInBand

Hope it helps!

Upvotes: 8

Related Questions