Alejandro Lora
Alejandro Lora

Reputation: 7802

Debugging Nest App using path mapping by TS

I am trying to debug a typescript-node app (by nestjs) but as I included the path mapping by Typescript ->

https://www.typescriptlang.org/docs/handbook/module-resolution.html#path-mapping

it doesn't work anymore, it throws this error:

enter image description here

Debug config file looks like this:

enter image description here

And TSCONFIG file looks like this:

enter image description here

Just to mention that the app works fine, the tests are passing fine and everything is working as expected, except when I press play to debug de app.

A work-around is to replace those paths by the relative normal path to be imported, but that means getting rid of the path mapping feature brought by TS and that's my last shot.

Upvotes: 5

Views: 3759

Answers (2)

Dzianis Ihnatovich
Dzianis Ihnatovich

Reputation: 53

As for debugging, I wonder why you've introduced nodemon in case of using typescript and having ts-node already installed. Your solution can be simplified using only tsconfig-paths lib. After installing, launch.json in vscode may be updated as follows:

{
  "configurations": [{
    "type": "node",
    "request": "launch",
    "name": "Debug Nest App",
    "args": ["src/main.ts"],
    "runtimeArgs": ["-r", "ts-node/register", "-r", "tsconfig-paths/register"],
    "autoAttachChildProcesses": true
  }]
}

Here is the link to that particular point in package documentation.

PS: Nest framework provides script start:debug out of the box which also can be simply attached to launch.json configuration.

Upvotes: 4

Alejandro Lora
Alejandro Lora

Reputation: 7802

I got the solution for this. I am going to detail the steps in case it helps someone else.

When adding path mapping to your project, you get the chance of using shorter and absolute paths to modules, which it has some pros/const but generally I think it's great when working with modules.

Problem might come when testing, debugging or running the app differently than when you work in dev mode.

So using jest, you need to add:

"jest": {
    "moduleFileExtensions": [ ... ],
    "moduleNameMapper": {
      "@db/(.*)": "<rootDir>/core/database/$1",
      "@exceptions/(.*)": "<rootDir>/core/exceptions/$1",
      "@permissions/(.*)": "<rootDir>/permissions/$1",
      "@roles/(.*)": "<rootDir>/roles/$1",
      "@users/(.*)": "<rootDir>/users/$1",
      "@videos/(.*)": "<rootDir>/videos/$1"
    },
    "rootDir": "src",
    ...

Then for debugging, I needed to do the following steps:

1) Update launch.json in vscode:

        {
            "type": "node",
            "request": "launch",
            "name": "Nest Debug",
            "runtimeExecutable": "npm",
            "runtimeArgs": [
                "run-script",
                "debug"
            ],
            "port": 9229
        },

2) Update package.json scripts to add:

"debug": "nodemon --config nodemon-debug.json",

3) Install tsconfig-paths - (npm install --save-dev tsconfig-paths)

https://github.com/dividab/tsconfig-paths

4) Create/Update nodemon-debug.json file:

{
  "watch": [
    "src"
  ],
  "ext": "ts",
  "ignore": [
    "src/**/*.spec.ts"
  ],
  "exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts"
}

Notice this line

node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts

Difference with nodemon.json is:

  • Nodemon.json: "exec": "ts-node -r tsconfig-paths/register src/main.ts"
  • Nodemon-debug.json: "exec": "ts-node -r tsconfig-paths/register -r tsconfig-paths/register src/main.ts"

Upvotes: 4

Related Questions