6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VS CodeでDockerで起動したNode.jsのアプリをデバッグする

Posted at

1. npm scriptsを定義

{
  "scripts": {
    "dev": "npx node-dev --inspect=0.0.0.0 index.js"
  }
}

などどする。
上記ではnode-devを使用しているが、代わりにnodeを使うときも同様に
--inspect=0.0.0.0を付ける。
デフォルトでポート9229でデバッガーが起動する。

2. Dockerとかdocker-composeで起動

DockerfileのEXPOSEやdocker-composeのportsで
ポート9229をリッスンできるようにする。

例.

  app:
    build: .
    command: npm run dev
    ports:
      - "3000:3000"
      - "9229:9229"

docker-compose up等して起動する。

3. VS Codeのlaunch.jsonに構成を追加

launch.jsonの設定画面で"構成の追加"ボタンからDocker: Attach to Nodeを選ぶことで追加できる。

SS_ 2018-04-18 15.37.29.png

remoteRootは任意に設定する。

例.

    {
      "type": "node",
      "request": "attach",
      "name": "Docker: Attach to Node",
      "port": 9229,
      "address": "localhost",
      "localRoot": "${workspaceFolder}",
      "remoteRoot": "/usr/src/app",
      "protocol": "inspector"
    }
6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?