We want to be able to set breakpoints in our code using VSCode’s built-in debugger, and have code execution stop at these breakpoints.

We need to add a launch configuration file at .vscode/launch.json:

{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Debug Express API",
      "type": "node",
      "request": "launch",
      "program": "${workspaceFolder}/src/index.ts",
      "runtimeExecutable": "tsx",
      "runtimeArgs": ["--inspect"],
      "env": {
        "NODE_ENV": "development",
        "PORT": "5000"
      },
      "console": "integratedTerminal",
      "restart": true,
      "sourceMaps": true,
      "resolveSourceMapLocations": [
        "${workspaceFolder}/**",
        "!**/node_modules/**"
      ]
    }
  ]
}

We would then start the dev server through the debug tab rather than npm run dev.



Repo link

Tags: