Nodejs

 

 

vscode  -> launch.json

{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug Nest Framework",
      "runtimeExecutable": "pnpm",
      "runtimeArgs": [
        "run",
        "start:debug",
        "--",
        "--inspect-brk"
      ],
      "autoAttachChildProcesses": true,
      "restart": true,
      "sourceMaps": true,
      "stopOnEntry": false,
      "console": "integratedTerminal"
    }
  ]
}

 

 

 

 

VSCode에서 NestJS 디버그 설정 (launch.json)

1. launch.json 생성

VSCode에서 디버그 탭(Ctrl + Shift + D)으로 이동한 후 "create a launch.json file" 버튼을 클릭하고 Node.js 환경을 선택합니다.

 

2. launch.json 설정

NestJS 애플리케이션을 디버깅하기 위해 launch.json을 아래와 같이 작성합니다.


{
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "launch",
      "name": "Debug NestJS",
      "runtimeArgs": ["--nolazy", "-r", "ts-node/register"],
      "program": "${workspaceFolder}/src/main.ts",
      "cwd": "${workspaceFolder}",
      "sourceMaps": true,
      "env": {
        "NODE_ENV": "development"
      },
      "outFiles": ["${workspaceFolder}/dist/**/*.js"],
      "console": "integratedTerminal",
      "autoAttachChildProcesses": true
    }
  ]
}

 

3. 실행 방법

  1. VSCode 디버그 탭(Ctrl + Shift + D)에서 "Debug NestJS" 선택
  2. F5 키를 눌러 디버깅 시작
  3. src/main.ts에서 실행이 시작되며, 브레이크포인트를 걸어 디버깅 가능

 

4. package.json 수정 (선택 사항)

NestJS는 ts-node를 사용하여 TypeScript를 실행하므로, package.json에 아래와 같은 스크립트를 추가하면 편리합니다.


"scripts": {
  "start:debug": "nest start --debug --watch"
}

그리고 launch.json에서 다음과 같이 processId를 지정하여 실행 중인 NestJS 프로세스에 연결할 수도 있습니다.

{
  "type": "node",
  "request": "attach",
  "name": "Attach to NestJS",
  "processId": "${command:PickProcess}",
  "restart": true,
  "sourceMaps": true,
  "outFiles": ["${workspaceFolder}/dist/**/*.js"]
}

 

 

5. 실행 모드에 따른 옵션

  • 파일 단위 디버깅: launch.json에서 program을 src/main.ts로 지정
  • Attach 모드 디버깅: nest start --debug를 실행한 후 VSCode에서 Attach 모드 실행
  • ESM 모드: type: "module"을 사용하는 경우 "runtimeArgs": ["--loader", "ts-node/esm"] 추가

 

 

 

 

 

 

about author

PHRASE

Level 60  라이트

죽어 석 잔 술이 살아 한 잔 술만 못하다 , 죽은 뒤에 아무리 정성을 들여도 살아 있을 때 조금 생각한 것만 못하다는 말.

댓글 ( 0)

댓글 남기기

작성