Search code examples
typescript

Debugging nest.js application with vscode


I am testing the nest.js framework but I am struggling to run it with VSCode so that I can properly debug my code. This is pretty much the same issue as described here Running nest.js from VS Code. However I made sure I am using the latest packages. I always get this error:

Error: Cannot find module 'cats/cats.module'
    at Function.Module._resolveFilename (module.js:485:15)
    at Function.Module._load (module.js:437:25)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (c:\Users\user\Documents\random-api\dist\app.module.js:11:26)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)

npm run start works completely perfect, but I want to debug the application with the VSCode IDE.

My package.json dependencies:

  "dependencies": {
    "@nestjs/common": "^4.6.6",
    "@nestjs/core": "^4.6.6",
    "@nestjs/microservices": "^4.6.6",
    "@nestjs/testing": "^4.6.6",
    "@nestjs/websockets": "^4.6.6",
    "reflect-metadata": "^0.1.12",
    "rxjs": "^5.5.7",
    "typescript": "^2.7.2"
  },
  "devDependencies": {
    "@types/express": "^4.11.1",
    "@types/jest": "^22.2.2",
    "@types/node": "^9.6.0",
    "@types/supertest": "^2.0.4",
    "jest": "^22.4.3",
    "nodemon": "^1.17.2",
    "prettier": "^1.11.1",
    "supertest": "^3.0.0",
    "ts-jest": "^22.4.2",
    "ts-node": "^5.0.1",
    "tsconfig-paths": "^3.1.3",
    "tslint": "5.9.1",
    "tslint-microsoft-contrib": "^5.0.3"
  },

My vscode's launch.json:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}\\dist\\main.js",
            "smartStep": true,
            "outFiles": [
                "${workspaceFolder}/dist/**/*.js"
            ]
        }
    ]
}

I tried the same launch.json with the typescript file as path, but that threw the same exception:

"program": "${workspaceFolder}\\src\\main.ts",

Solution

  • Try this launch.json:

    {
        // Use IntelliSense to learn about possible attributes.
        // Hover to view descriptions of existing attributes.
        // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
        "version": "0.2.0",
        "configurations": [
            {
                "type": "node",
                "request": "launch",
                "name": "Debug Nest Framework",
                "args": ["${workspaceFolder}/src/main.ts"],
                "runtimeArgs": ["--nolazy", "-r", "ts-node/register", "-r", "tsconfig-paths/register"],
                "sourceMaps": true,
                "cwd": "${workspaceRoot}",
                "protocol": "inspector"
            }
        ]
    }