VSCode에서 NestJs 디버깅

배고픈 징징이 ㅣ 2024. 7. 4. 16:49

1. 서론

launch.json 파일을 생성해서 디버깅을 하다가, 예외 발생시 예외 로그가 나오지 않는 이슈가 발생했다.

이에 다른 방법을 찾다가 터미널로 쉽게 디버깅이 가능하다는 걸 알게되어 수정했다.

일단 두 방법 다 설명을 하겠다.

 

2. JavaScript Debug Terminal

1. VSCode에서 터미널 화면을 켠다.

2. 탭바 오른쪽에 있는 + 버튼 옆 Launch Profile ... 을 선택하여 JavaSCript Dubug Terminal을 켠다.

3. JavaSCript Dubug Terminal에서 프로젝트를 실행시킨다. npm run (script)

4. 디버깅이 된다.

 

3. Lanch.Json

1. launch.json 파일 생성

 

VSCode에서 F5를 눌러서 생성하거나
Ctrl + Shift + D를 눌러서 Run and Debug 창을 켜 생성한다.
Debugger는 Node.js로 선택한다.

생성을 하면 아래 코드와 같이 초기 세팅이 되어있다.

 

{
    // 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": "Launch Program",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${file}",
            "preLaunchTask": "tsc: build - tsconfig.json",
            "outFiles": [
                "${workspaceFolder}/dist/**/*.js"
            ]
        }
    ]
}

 

 

2. launch.json 파일 수정

 

생성된 launch.json 파일을 프로젝트에 맞게 수정한다.

type : 우리가 선택한 Debugger (node.js)

request : 디버그 세션의 요청 유형

name : 디버그의 이름 설정

skipFiles : 디버깅 중 건너뛸 파일

program : 실행할 파일의 경로 (main.ts의 경로)

 

{
    // 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": "AdminPortal Be Debug",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}\\src\\main.ts",
        }
    ]
}

 

 

3. 디버깅

 

위와 같이 세팅을 하고 F5를 눌러 디버깅을 시작.

NestJs 코드에서 브레이크 포인트를 지정.

화면단에서 테스트를 해보면 지정된 브레이크 포인트에서 멈추면서 디버깅을 할 수 있다.

 

참고로 NestJs를 실행중에 있다면, 켜놓은 NestJs를 끄고 디버깅을 시작해야 한다.

포트 충돌로 디버깅이 켜지지 않을 것이다.

반응형

'NodeJS' 카테고리의 다른 글

2.Screen Sharing - Sharing  (0) 2024.08.09
1. Screen Sharing - SocketIO Setting  (0) 2024.08.09
NextJs  (0) 2024.07.23
Express & NestJS  (0) 2024.07.16
NVM (Node Version Manager)  (0) 2024.06.27