Correct way to debug Node.js project with TypeScript and Webpack in Visual Studio Code


Sergey Sovgut

The problem is described in the topic title.

File structure:

-source
   -app
      -tools
         Cache.ts
         Logger.ts
      databases.ts
      filesystem.ts
      library.ts
      runtime.ts
   -config
      filesystem.ts
      service.ts
   service.ts

Compile file:

-bin
   service.bundle.js
   service.bundle.js.map

package.json:

{
  "name": "service",
  "scripts": {
    "start": "node bin/service.bundle",
    "dev": "webpack --watch",
    "debug-build": "tsc"
  },
  "private": true,
  "devDependencies": {
    "eslint": "^5.13.0",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-config-prettier": "^4.0.0",
    "eslint-plugin-import": "^2.16.0",
    "eslint-plugin-jsx-a11y": "^6.2.1",
    "eslint-plugin-prettier": "^3.0.1",
    "eslint-plugin-react": "^7.12.4",
    "prettier": "^1.16.4",
    "ts-loader": "^5.3.3",
    "typescript": "^3.3.3",
    "webpack": "^4.29.3",
    "webpack-cli": "^3.2.3"
  },
  "dependencies": {
    "@types/mkdirp": "^0.5.2",
    "@types/node": "^11.9.3",
    "mkdirp": "^0.5.1"
  }
}

task.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Webpack watch",
      "type": "npm",
      "script": "dev"
    },
    {
      "label": "Webpack build",
      "type": "npm",
      "script": "build"
    },
    {
      "label": "Debug",
      "type": "npm",
      "script": "start"
    }
  ]
}

launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",

      "stopOnEntry": false,
      "args": [],
      "cwd": "${workspaceFolder}",
      "preLaunchTask": null,
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "start"],
      "env": {
        "NODE_ENV": "development"
      },
      "console": "integratedTerminal",
      "port": 9229
    }
  ]
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "rootDir": "./source",
    "removeComments": true,
    "downlevelIteration": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}

webpack.config.js:

const path = require('path');
const webpack = require('webpack');

module.exports = {
  entry: ['./source/service.ts'],
  target: 'node',
  module: {
    rules: [
      {
        test: /.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  mode: 'development',
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },
  plugins: [
    new webpack.SourceMapDevToolPlugin({
      filename: 'service.bundle.js.map'
    })
  ],
  output: {
    path: path.join(__dirname, 'bin'),
    filename: 'service.bundle.js'
  }
};

I spent a lot of time looking for a good solution for debugging a project on nodejs using typescript and webpack compiled into one file.

First, I start webpack, then start the debugger. Absolutely all solutions don't work for unknown reasons :( Debugger in Visual Studio Code doesn't want to work in any way.

[Solved] Debug the project with the following configuration:

launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Launch Program",
      "program": "${workspaceFolder}/source/service.ts",
      "outFiles": ["${workspaceFolder}/debug/**/*.js"],
      "preLaunchTask": "Build",
      "env": {
        "NODE_ENV": "development"
      },
      "console": "integratedTerminal"
    }
  ]
}

task.json:

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Webpack watch",
      "type": "npm",
      "script": "dev"
    },
    {
      "label": "Build",
      "type": "npm",
      "script": "debug-build"
    }
  ]
}

package.json:

{
  "name": "service",
  "scripts": {
    "start": "node bin/service.bundle",
    "dev": "webpack --watch",
    "debug-build": "tsc"
  },
  "private": true,
  "devDependencies": {
    "eslint": "^5.13.0",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-config-prettier": "^4.0.0",
    "eslint-plugin-import": "^2.16.0",
    "eslint-plugin-jsx-a11y": "^6.2.1",
    "eslint-plugin-prettier": "^3.0.1",
    "eslint-plugin-react": "^7.12.4",
    "prettier": "^1.16.4",
    "ts-loader": "^5.3.3",
    "typescript": "^3.3.3",
    "webpack": "^4.29.3",
    "webpack-cli": "^3.2.3"
  },
  "dependencies": {
    "@types/mkdirp": "^0.5.2",
    "@types/node": "^11.9.3",
    "mkdirp": "^0.5.1"
  }
}

tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./debug",
    "rootDir": "./source",
    "removeComments": true,
    "downlevelIteration": true,
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true
  }
}

webpack.config.js

const path = require('path');

module.exports = {
  entry: ['./source/service.ts'],
  target: 'node',
  module: {
    rules: [
      {
        test: /.tsx?$/,
        use: 'ts-loader',
        exclude: /node_modules/
      }
    ]
  },
  mode: 'development',
  resolve: {
    extensions: ['.tsx', '.ts', '.js']
  },
  output: {
    path: path.join(__dirname, 'bin'),
    filename: 'service.bundle.js'
  }
};
Juradev

You need to add program properties to launch.json

your starting point

 "program": "${workspaceFolder}/source/service.ts",

and another prop trumps

"outFiles": [
    "${workspaceFolder}/bin/**/*.js"
  ]

I'm not sure if the source map works with breakpoints. If not, don't use plugin use devtool prop in webpack...

devtool: 'source-map',

Related


How to debug typescript files in visual studio code

MonkeyBonkey: Using version 0.3 of Visual Studio Code, I'm not sure how to enable Sourcemap and debug ts files I am getting the following errorcan't launch program '/Projects/app-server/server.ts'; enabling source maps might help How to enable Sourcemap and Ty

How to debug child Node.JS process in Visual Studio Code?

Vinianski How to debug child Node.JS processes in VS Code? Here is an example of the code I am trying to debug: var spawn = require('child_process').spawn; var scriptPath = './child-script.js'; var runner_ = spawn('node', [scriptPath]); Benjamin Passero You c

Debug node.js in Visual Studio Code preview on OS X

Lone Ranger I'm trying the newly released preview of Visual Studio Code on Mac OS X and I get the error "Cannot start OpenDebug due to Mono (or Mono version >= 3.10.0) when trying to start debugging or attaching to my node.js app , so OpenDebug cannot be start

Visual Studio Code - Debug Node JS via TypeScript

Brutus I'm currently trying to debug a Node JS application written in TypeScript from Visual Studio Code and I'm running into some issues. I have a situation similar to the one described in this question |-- .settings |----- launch.json |-- bin |----- app.js |

How to create conditional debug visual studio code using node js

Basil Battikhi I have a loop that has a huge iteration of about 300 iterations, which may be small, but I'm having trouble debugging the loop, let's look at the code below (example, it's not exactly part of the code) . const myVar = [ { name: 'basil', age: 2

Debug node.js in Visual Studio Code preview on OS X

Lone Ranger I'm trying the newly released Visual Studio Code preview on Mac OS X and I get the error "Cannot start OpenDebug due to Mono (or Mono version >= 3.10.0, Therefore cannot start OpenDebug)) is required". I have installed Mono MDK from http://www.mono

Visual Studio Code - Debug Node JS via TypeScript

Brutus I'm currently trying to debug a Node JS application written in TypeScript from Visual Studio Code and I'm running into some issues. I have a situation similar to the one described in this question |-- .settings |----- launch.json |-- bin |----- app.js |

How to debug typescript files in visual studio code

MonkeyBonkey: Using version 0.3 of Visual Studio Code, I'm not sure how to enable Sourcemap and debug ts files I am getting the following errorcan't launch program '/Projects/app-server/server.ts'; enabling source maps might help How to enable Sourcemap and Ty

Debug TypeScript code with Visual Studio

multiple sclerosis Is there a way to debug TypeScript source (instead of generated javascript) in Visual Studio? According to the TypeScript language specification: TypeScript can optionally provide source maps, enabling source-level debugging. So I expected t

How to debug child Node.JS process in Visual Studio Code?

Vinianski How to debug child Node.JS processes in VS Code? Here is an example of the code I am trying to debug: var spawn = require('child_process').spawn; var scriptPath = './child-script.js'; var runner_ = spawn('node', [scriptPath]); Benjamin Passero You c

Debug Node js with Visual Studio Code

Jacabello I tried to debug the project in node JS using Visual Studio Code but it didn't work. I built a simple project with: express myExpressApp cd myExpressApp npm install npm start My file launch.json: I select the "Launch app.js" option in the "Debug" win

Debug node.js in Visual Studio Code preview on OS X

Lone Ranger I'm trying the newly released Visual Studio Code preview on Mac OS X and I get the error "Cannot start OpenDebug due to Mono (or Mono version >= 3.10.0) when trying to start debugging or attaching to my node.js app , so OpenDebug cannot be started)

Visual Studio Code - Debug Node JS via TypeScript

Brutus I'm currently trying to debug a Node JS application written in TypeScript from Visual Studio Code and I'm running into some issues. I have a situation similar to the one described in this question |-- .settings |----- launch.json |-- bin |----- app.js |

Visual Studio Code - Debug Node JS via TypeScript

Brutus I'm currently trying to debug a Node JS application written in TypeScript from Visual Studio Code and I'm running into some issues. I have a situation similar to the one described in this question |-- .settings |----- launch.json |-- bin |----- app.js |

Visual Studio Code - Debug Node JS via TypeScript

Brutus I'm currently trying to debug a Node JS application written in TypeScript from Visual Studio Code and I'm running into some issues. I have a situation similar to the one described in this question |-- .settings |----- launch.json |-- bin |----- app.js |

Visual Studio Code - Debug Node JS via TypeScript

Brutus I'm currently trying to debug a Node JS application written in TypeScript from Visual Studio Code and I'm running into some issues. I have a situation similar to the one described in this question |-- .settings |----- launch.json |-- bin |----- app.js |

How to create conditional debug visual studio code using node js

Basil Battikhi I have a loop that has a huge iteration of about 300 iterations, which may be small, but I'm having trouble debugging the loop, let's look at the code below (example, it's not exactly part of the code) . const myVar = [ { name: 'basil', age: 2

Debug node.js in Visual Studio Code preview on OS X

Lone Ranger I'm trying the newly released preview of Visual Studio Code on Mac OS X and I get the error "Cannot start OpenDebug due to Mono (or Mono version >= 3.10.0, Therefore cannot start OpenDebug)) is required". I have installed Mono MDK from http://www.m

How to debug typescript files in visual studio code

MonkeyBonkey: Using version 0.3 of Visual Studio Code, I'm not sure how to enable Sourcemap and debug ts files I am getting the following errorcan't launch program '/Projects/app-server/server.ts'; enabling source maps might help How to enable Sourcemap and Ty

Debug TypeScript code with Visual Studio

multiple sclerosis Is there a way to debug TypeScript source (instead of generated javascript) in Visual Studio? According to the TypeScript language specification: TypeScript can optionally provide source maps, enabling source-level debugging. So I expected t

Debug Node js with Visual Studio Code

Jacabello I tried to debug the project in node JS using Visual Studio Code but it didn't work. I built a simple project with: express myExpressApp cd myExpressApp npm install npm start My file launch.json: I select the "Launch app.js" option in the "Debug" win

Debug node.js in Visual Studio Code preview on OS X

Lone Ranger I'm trying the newly released Visual Studio Code preview on Mac OS X and I get the error "Cannot start OpenDebug due to Mono (or Mono version >= 3.10.0) when trying to start debugging or attaching to my node.js app , so OpenDebug cannot be started)

Visual Studio Code - Debug Node JS via TypeScript

Brutus I'm currently trying to debug a Node JS application written in TypeScript from Visual Studio Code and I'm running into some issues. I have a situation similar to the one described in this question |-- .settings |----- launch.json |-- bin |----- app.js |

Visual Studio Code - Debug Node JS via TypeScript

Brutus I'm currently trying to debug a Node JS application written in TypeScript from Visual Studio Code and I'm running into some issues. I have a situation similar to the one described in this question |-- .settings |----- launch.json |-- bin |----- app.js |

Visual Studio Code - Debug Node JS via TypeScript

Brutus I'm currently trying to debug a Node JS application written in TypeScript from Visual Studio Code and I'm running into some issues. I have a situation similar to the one described in this question |-- .settings |----- launch.json |-- bin |----- app.js |

How to create conditional debug visual studio code using node js

Basil Battikhi I have a loop that has a huge iteration of about 300 iterations, which may be small, but I'm having trouble debugging the loop, let's look at the code below (example, it's not exactly part of the code) . const myVar = [ { name: 'basil', age: 2

How to debug child Node.JS process in Visual Studio Code?

Vinianski How to debug child Node.JS processes in VS Code? Here is an example of the code I am trying to debug: var spawn = require('child_process').spawn; var scriptPath = './child-script.js'; var runner_ = spawn('node', [scriptPath]); Benjamin Passero You c