Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Đã đăng vào thg 12 8, 2021 3:30 SA 3 phút đọc 3 phút đọc

Debug nodejs app với Visual Studio Code

Trong bài viết này mình sẽ hướng dẫn debug nodejs app với framework ExpressJS. Đối với các framework, ngôn ngữ khác các bạn có thể tìm hiểu và config tương tự

1. Chuẩn bị môi trường

a. Express app

  • Cấu trúc thư mục của mình như sau:

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

  • Thư viện cần cài đăt: express. Để cài đặt thư viện các bạn vào terminal của thư mục hiện tại và thực hiện lệnhexpress. Để cài đặt thư viện các bạn vào terminal của thư mục hiện tại và thực hiện lệnh

    npm i express
    
  • Mã nguồn file index.js

    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
  • Mình lấy ví dụ getting started trên trang chủ của Express. Các bạn có thể tham khảo thêm tại đây.getting started trên trang chủ của Express. Các bạn có thể tham khảo thêm tại đây.

b. Thêm cấu hình debug

  • Để thực hiện debug, chúng ta vào phần Run and Debug trên VSCode -> chọn create a launch.json file -> Chọn Nodejscreate a launch.json file -> Chọn Nodejs

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

  • VSCode sẽ tự sinh cho chúng ta một file là
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    1 bao gồm các khai báo cho việc debug. Nội dung file như sau
    {
      // 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": "pwa-node",
          "request": "launch",
          "name": "Launch Program",
          "skipFiles": [
            "/**"
          ],
          "program": "${workspaceFolder}\\index.js"
        }
      ]
    }
    
  • Một số thông tin quan trọng:
    • "type": "pwa-node" -> sử dụng Javascript Debugger
    • "name": "Launch program" -> Tên của luồng chạy debug. Có thể đổi tên thành debug nodejs cho dễ hiểu 😀debug nodejs cho dễ hiểu 😀
    • "program": "${workspaceFolder}\index.js". -> khai báo đầu vào của luồng debug. Ở đây chương trình của mình chạy đầu vào ở file index.js nên sẽ khai báo như trên.
        "debug.javascript.terminalOptions": {
          "skipFiles": [
            "/**"
          ]
        },
      
      2 trỏ đến thư mục làm việc hiện tại của chương trình.

2. Thực hiện debug

a. Chọn breakpoint

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

  • Như hình trên, mình chọn breakpoint ở dòng số 6 để kiểm tra giá trị của biến message khi thực hiện debug

b. Thực hiện debug

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

  • Chọn biểu tượng phần debug
  • Chọn tác vụ debug có tên trùng với phần name đã khai báo trong file launch.json
  • Nhấn vào biểu tượng debug(hình tam giác màu xanh) để thực hiện debug
  • Bây giờ chúng ta có thể vào trình duyệt, gõ địa chỉ
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    3
  • Chương trình sẽ được gọi và dừng lại tại điểm breakpoint đã đặt trước
  • Giờ chúng ta có thể nhấn F10 để chạy qua dòng khai báo giá trị của biến
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    4 và xem giá trị của biến là Hello World! khi trỏ vào cũng như bảng giá trị local ở bên cạnhHello World! khi trỏ vào cũng như bảng giá trị local ở bên cạnh

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Các thao tác debug khác như

  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
5, hay như các config khác các bạn có thể tham khảo thêm trên document của visual code tại đây!

All rights reserved

Node.js gỡ lỗi trong mã vs

Trình chỉnh sửa mã Visual Studio đã hỗ trợ gỡ lỗi tích hợp cho thời gian chạy Node.js và có thể gỡ lỗi JavaScript, TypeScript và nhiều ngôn ngữ khác được truyền vào JavaScript. Thiết lập một dự án để gỡ lỗi Node.js rất đơn giản với mã VS cung cấp các mặc định và đoạn trích cấu hình khởi chạy phù hợp.

Có một vài cách bạn có thể gỡ lỗi các chương trình Node.js trong mã vs:

  • Sử dụng Auto đính kèm để gỡ lỗi các quy trình bạn chạy trong thiết bị đầu cuối tích hợp của VS Code.
  • Sử dụng thiết bị đầu cuối gỡ lỗi JavaScript, tương tự như sử dụng thiết bị đầu cuối tích hợp.
  • Sử dụng cấu hình khởi chạy để bắt đầu chương trình của bạn hoặc đính kèm vào một quy trình được khởi chạy bên ngoài mã VS.

Đính kèm tự động

Nếu tính năng Auto đính kèm được bật, trình gỡ lỗi nút sẽ tự động gắn vào các quy trình nhất định của Node.js đã được khởi chạy từ thiết bị đầu cuối tích hợp của mã vs. Để bật tính năng, sử dụng lệnh Toggle Auto đính kèm từ bảng lệnh (⇧⌘P (Windows, Linux Ctrl+Shift+P)) hoặc, nếu nó đã được kích hoạt, hãy sử dụng mục thanh trạng thái Auto đính kèm.Auto Attach feature is enabled, the Node debugger automatically attaches to certain Node.js processes that have been launched from VS Code's Integrated Terminal. To enable the feature, either use the Toggle Auto Attach command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) or, if it's already activated, use the Auto Attach Status bar item.

Có ba chế độ cho Auto đính kèm, bạn có thể chọn trong Pick Pick nhanh chóng và thông qua cài đặt Debug.JavaScript.Autoattachfilter:debug.javascript.autoAttachFilter setting:

  •   "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    6 (mặc định) - Nếu bạn thực thi tập lệnh bên ngoài thư mục
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    7 của bạn hoặc sử dụng tập lệnh 'Runner' chung như Mocha hoặc TS -Node, quy trình sẽ được gỡ lỗi. Bạn có thể định cấu hình tập lệnh 'Runner' cho phép bằng cách sử dụng cài đặt mẫu thông minh Auto đính kèm (
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    8).Auto Attach Smart Pattern setting (
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    8).
  •   "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    9 - Tất cả các quy trình Node.js được khởi chạy trong thiết bị đầu cuối tích hợp sẽ được gỡ lỗi.
  • [
      '!**/node_modules/**', // exclude scripts in node_modules folders
      '**/$KNOWN_TOOLS$/**' // but include some common tools
    ];
    
    0 - Chỉ các quy trình được khởi chạy với cờ
    [
      '!**/node_modules/**', // exclude scripts in node_modules folders
      '**/$KNOWN_TOOLS$/**' // but include some common tools
    ];
    
    1 hoặc
    [
      '!**/node_modules/**', // exclude scripts in node_modules folders
      '**/$KNOWN_TOOLS$/**' // but include some common tools
    ];
    
    2 mới được gỡ lỗi.

Sau khi bật Auto đính kèm, bạn sẽ cần khởi động lại thiết bị đầu cuối của mình. Điều này có thể được thực hiện bằng cách nhấp vào biểu tượng ⚠ ở phía trên bên phải của thiết bị đầu cuối hoặc chỉ tạo một biểu tượng mới. Sau đó, trình gỡ lỗi nên gắn vào chương trình của bạn trong vòng một giây:Auto Attach, you'll need to restart your terminal. This can be done by clicking the ⚠ icon in the top right of the terminal, or just creating a new one. Then, the debugger should attach to your program within a second:

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Khi Auto đính kèm được bật, mục

[
  '!**/node_modules/**', // exclude scripts in node_modules folders
  '**/$KNOWN_TOOLS$/**' // but include some common tools
];
3 sẽ xuất hiện trong thanh trạng thái bên dưới cùng của cửa sổ mã VS. Nhấp vào nó cho phép bạn thay đổi chế độ đính kèm tự động hoặc tạm thời tắt nó. Tạm thời tắt Auto đính kèm rất hữu ích nếu bạn đang chạy một số chương trình một lần mà bạn không cần gỡ lỗi, nhưng bạn không muốn vô hiệu hóa hoàn toàn tính năng.

Cấu hình bổ sung

Các thuộc tính cấu hình khởi chạy khác

Bạn có thể áp dụng các thuộc tính khác thường được tìm thấy trong Launch.json để tự động đính kèm trong cài đặt DEBUG.JAVASCRIPT.TerminalOptions. Ví dụ: để thêm nút nội bộ vào SkipFiles của bạn, bạn có thể thêm phần sau vào cài đặt người dùng hoặc không gian làm việc của mình:debug.javascript.terminalOptions setting. For example, to add node internals to your skipFiles, you could add the following to your user or workspace settings:

  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },

Tự động đính kèm các mẫu thông minh

Trong chế độ

  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
6 Auto đính kèm, mã VS sẽ cố gắng đính kèm vào mã của bạn và không đính kèm với các công cụ xây dựng mà bạn không quan tâm đến việc gỡ lỗi. Nó thực hiện điều này bằng cách kết hợp tập lệnh chính với một danh sách các mẫu GLOB. Các mẫu GLOB có thể định cấu hình trong cài đặt Debug.JavaScript.AutoattachSmartPotype, mặc định là:debug.javascript.autoAttachSmartPattern setting, which defaults to:

[
  '!**/node_modules/**', // exclude scripts in node_modules folders
  '**/$KNOWN_TOOLS$/**' // but include some common tools
];

[
  '!**/node_modules/**', // exclude scripts in node_modules folders
  '**/$KNOWN_TOOLS$/**' // but include some common tools
];
5 được thay thế bằng một danh sách các 'người chạy mã' phổ biến như
[
  '!**/node_modules/**', // exclude scripts in node_modules folders
  '**/$KNOWN_TOOLS$/**' // but include some common tools
];
6,
[
  '!**/node_modules/**', // exclude scripts in node_modules folders
  '**/$KNOWN_TOOLS$/**' // but include some common tools
];
7,
[
  '!**/node_modules/**', // exclude scripts in node_modules folders
  '**/$KNOWN_TOOLS$/**' // but include some common tools
];
8, v.v. Bạn có thể sửa đổi danh sách này nếu các cài đặt này không hoạt động. Ví dụ: để loại trừ
[
  '!**/node_modules/**', // exclude scripts in node_modules folders
  '**/$KNOWN_TOOLS$/**' // but include some common tools
];
7 và bao gồm
[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
0, bạn có thể thêm hai dòng:

[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];

Thiết bị đầu cuối gỡ lỗi JavaScript

Theo cách tương tự với Auto đính kèm, thiết bị đầu cuối gỡ lỗi JavaScript sẽ tự động gỡ lỗi bất kỳ quy trình Node.js nào bạn chạy trong đó. Bạn có thể tạo một thiết bị đầu cuối gỡ lỗi bằng cách chạy gỡ lỗi: Tạo lệnh thiết bị đầu cuối gỡ lỗi JavaScript từ bảng lệnh (

[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
1) hoặc bằng cách chọn thiết bị đầu cuối gỡ lỗi JavaScript từ thả xuống trình chuyển đổi thiết bị đầu cuối.Debug: Create JavaScript Debug Terminal command from the Command Palette (
[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
1), or by selecting the Create JavaScript Debug Terminal from the terminal switcher dropdown.

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Cấu hình bổ sung

Các thuộc tính cấu hình khởi chạy khác

Bạn có thể áp dụng các thuộc tính khác thường được tìm thấy trong Launch.json cho thiết bị đầu cuối gỡ lỗi trong cài đặt Debug.JavaScript.TerminalOptions. Ví dụ: để thêm nút nội bộ vào SkipFiles của bạn, bạn có thể thêm phần sau vào cài đặt người dùng hoặc không gian làm việc của mình:debug.javascript.terminalOptions setting. For example, to add node internals to your skipFiles, you could add the following to your user or workspace settings:

"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},

Khởi chạy cấu hình

Các cấu hình khởi chạy là cách truyền thống để thiết lập gỡ lỗi trong mã VS và cung cấp cho bạn các tùy chọn cấu hình nhất để chạy các ứng dụng phức tạp.

Trong phần này, chúng tôi sẽ đi sâu vào chi tiết hơn về các cấu hình và tính năng cho các kịch bản gỡ lỗi nâng cao hơn. Bạn sẽ tìm thấy hướng dẫn gỡ lỗi với bản đồ nguồn, bước qua mã bên ngoài, gỡ lỗi từ xa và nhiều hơn nữa.

Nếu bạn muốn xem một video giới thiệu, hãy xem bắt đầu với gỡ lỗi Node.js.

Lưu ý: Nếu bạn mới bắt đầu với mã VS, bạn có thể tìm hiểu về các tính năng gỡ lỗi chung và tạo các tệp cấu hình

  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
1 trong chủ đề gỡ lỗi.: If you are just getting started with VS Code, you can learn about general debugging features and creating
  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
1 configuration files in the Debugging topic.

Khởi chạy các thuộc tính cấu hình

Các cấu hình gỡ lỗi được lưu trữ trong tệp

  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
1 nằm trong thư mục
[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
4 của không gian làm việc của bạn. Giới thiệu về việc tạo và sử dụng các tệp cấu hình gỡ lỗi nằm trong bài viết gỡ lỗi chung.

Dưới đây là một tham chiếu của các thuộc tính

  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
1 phổ biến cụ thể cho Trình gỡ lỗi Node.js. Bạn có thể xem tập hợp hoàn chỉnh các tùy chọn trong tài liệu Tùy chọn VSCode-JS-Debug.

Các thuộc tính sau được hỗ trợ trong các cấu hình khởi chạy loại

[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
6 và
[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
7:

  • [
      '!**/node_modules/**',
      '**/$KNOWN_TOOLS$/**',
      '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
      '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
    ];
    
    8 - Mảng các mẫu GLOB để định vị các tệp JavaScript được tạo. Xem phần Bản đồ nguồn.
  • [
      '!**/node_modules/**',
      '**/$KNOWN_TOOLS$/**',
      '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
      '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
    ];
    
    9 - Một loạt các mẫu glob cho các vị trí mà các bản đồ nguồn nên được phân tích cú pháp. Xem phần Bản đồ nguồn.
  • "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    0 - Khi khởi động lại một phiên, hãy từ bỏ sau số mili giây này. Xem phần gắn vào Node.js.
  • "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    1 - phá vỡ ngay lập tức khi chương trình khởi chạy.
  • "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    2 - Thư mục gốc của mã vs. Xem phần gỡ lỗi từ xa bên dưới.
  • "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    3 - Thư mục gốc của nút. Xem phần gỡ lỗi từ xa bên dưới.
  • ________ 64- Cố gắng tự động bước qua mã không ánh xạ tới các tệp nguồn. Xem phần bước thông minh.
  • "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    5 - Tự động bỏ qua các tệp được bao phủ bởi các mẫu GLOB này. Xem phần Bỏ qua mã không thú vị.
  • "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    6 - Kích hoạt đầu ra chẩn đoán.

Các thuộc tính này chỉ có sẵn cho các cấu hình khởi chạy của loại yêu cầu

[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
6:

  • "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    8 - Một đường dẫn tuyệt đối đến chương trình Node.js để gỡ lỗi.
  • "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    9 - Đối số được chuyển cho chương trình để gỡ lỗi. Thuộc tính này là loại mảng và mong đợi các đối số riêng lẻ là các phần tử mảng.
  •   "scripts": {
        "debug": "node myProgram.js"
      },
    
    0 - Khởi chạy chương trình để gỡ lỗi trong thư mục này.
  •   "scripts": {
        "debug": "node myProgram.js"
      },
    
    1 - Đường dẫn tuyệt đối đến thực thi thời gian chạy được sử dụng. Mặc định là
      "scripts": {
        "debug": "node myProgram.js"
      },
    
    2. Xem phần Hỗ trợ cấu hình khởi chạy cho 'NPM' và các công cụ khác.
  •   "scripts": {
        "debug": "node myProgram.js"
      },
    
    3 - Các đối số tùy chọn được chuyển cho thời gian chạy thực thi.
  •   "scripts": {
        "debug": "node myProgram.js"
      },
    
    4 - Nếu "NVM" (hoặc "NVM -Windows") hoặc "NVS" được sử dụng để quản lý các phiên bản Node.js, thuộc tính này có thể được sử dụng để chọn một phiên bản cụ thể của Node.js. Xem Phần hỗ trợ đa phiên bản bên dưới.
  •   "scripts": {
        "debug": "node myProgram.js"
      },
    
    5 - Biến môi trường tùy chọn. Thuộc tính này mong đợi các biến môi trường như một danh sách các cặp khóa/giá trị được gõ chuỗi.
  •   "scripts": {
        "debug": "node myProgram.js"
      },
    
    6 - Đường dẫn tùy chọn đến một tệp chứa các định nghĩa biến môi trường. Xem phần Biến môi trường tải từ tệp bên ngoài bên dưới.
  •   "scripts": {
        "debug": "node myProgram.js"
      },
    
    7 - Bảng điều khiển để khởi chạy chương trình (
      "scripts": {
        "debug": "node myProgram.js"
      },
    
    8,
      "scripts": {
        "debug": "node myProgram.js"
      },
    
    9,
    {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    0). Xem phần Bảng điều khiển nút bên dưới.
  • {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    1 - Nếu được đặt thành
    {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    2, đầu ra từ quá trình stdout/stderr sẽ được hiển thị trong bảng điều khiển gỡ lỗi, thay vì nghe đầu ra qua cổng gỡ lỗi. Điều này hữu ích cho các chương trình hoặc thư viện nhật ký ghi trực tiếp vào các luồng stdout/stderr thay vì sử dụng API
    {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    3.

Thuộc tính này chỉ có sẵn cho các cấu hình khởi chạy của loại yêu cầu

[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
7:

  • {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    5 - Khởi động lại kết nối khi chấm dứt. Xem phần Khởi động lại phiên gỡ lỗi tự động.
  • {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    6 - Cổng gỡ lỗi để sử dụng. Xem các phần gắn vào Node.js và gỡ lỗi từ xa.
  • {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    7 - Địa chỉ TCP/IP của cổng gỡ lỗi. Xem các phần gắn vào Node.js và gỡ lỗi từ xa.
  • {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    8 - Trình gỡ lỗi cố gắng đính kèm quy trình này sau khi đã gửi tín hiệu USR1. Với cài đặt này, trình gỡ lỗi có thể gắn vào một quá trình đã chạy không được bắt đầu ở chế độ gỡ lỗi. Khi sử dụng thuộc tính
    {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    8, cổng gỡ lỗi được xác định tự động dựa trên phiên bản Node.js (và giao thức đã sử dụng) và không thể được cấu hình rõ ràng. Vì vậy, đừng chỉ định thuộc tính
    {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    6.
  • {
      "type": "node",
      "request": "launch",
      "name": "Launch test",
      "runtimeVersion": "14",
      "program": "${workspaceFolder}/test.js"
    }
    
    1 - Có nên tiếp tục quá trình nếu nó bị tạm dừng khi chúng ta gắn bó với nó. Tùy chọn này rất hữu ích nếu bạn khởi chạy chương trình của mình với
    [
      '!**/node_modules/**', // exclude scripts in node_modules folders
      '**/$KNOWN_TOOLS$/**' // but include some common tools
    ];
    
    2.

Khởi chạy cấu hình cho các tình huống phổ biến

Bạn có thể kích hoạt IntelliSense (⌃Space (Windows, Linux Ctrl+Space)) trong tệp

  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
1 của bạn để xem các đoạn cấu hình khởi chạy cho các kịch bản gỡ lỗi nút thường được sử dụng.⌃Space (Windows, Linux Ctrl+Space)) in your
  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
1 file to see launch configuration snippets for commonly used Node.js debugging scenarios.

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Bạn cũng có thể hiển thị các đoạn trích với nút Thêm cấu hình ... ở phía dưới bên phải của cửa sổ Trình chỉnh sửa

  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
1.Add Configuration... button in the lower right of the
  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
1 editor window.

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Đây là những đoạn trích có sẵn:

  • Chương trình khởi động: Khởi chạy chương trình Node.js ở chế độ gỡ lỗi.: Launch a Node.js program in debug mode.
  • Khởi chạy qua NPM: Khởi chạy chương trình Node.js thông qua tập lệnh 'gỡ lỗi' NPM. Nếu bạn đã xác định tập lệnh gỡ lỗi NPM trong gói của mình.json, bạn có thể sử dụng nó trực tiếp từ cấu hình khởi chạy của mình. Đảm bảo rằng cổng gỡ lỗi được sử dụng trong tập lệnh NPM, tương ứng với cổng được chỉ định trong đoạn trích.: Launch a Node.js program through an npm 'debug' script. If you have defined an npm debug script in your package.json, you can use it directly from your launch configuration. Make sure that the debug port used in the npm script, corresponds to the port specified in the snippet.
  • Đính kèm: Đính kèm vào cổng gỡ lỗi của chương trình Node.js đang chạy cục bộ. Đảm bảo rằng chương trình Node.js để gỡ lỗi đã được bắt đầu ở chế độ gỡ lỗi và cổng gỡ lỗi được sử dụng giống như chương trình được chỉ định trong đoạn trích.: Attach to the debug port of a locally running Node.js program. Make sure that the Node.js program to debug has been started in debug mode, and the debug port used is the same as the one specified in the snippet.
  • Gắn vào chương trình từ xa: Gắn vào cổng gỡ lỗi của chương trình Node.js chạy trên máy chủ được chỉ định bởi thuộc tính
    {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    7. Đảm bảo rằng chương trình Node.js để gỡ lỗi đã được bắt đầu ở chế độ gỡ lỗi và cổng gỡ lỗi được sử dụng giống như chương trình được chỉ định trong đoạn trích. Để giúp các tệp nguồn bản đồ VS giữa không gian làm việc của bạn và hệ thống tập tin của máy chủ từ xa, hãy đảm bảo chỉ định các đường dẫn chính xác cho các thuộc tính
    "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    2and
    "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    3.
    : Attach to the debug port of a Node.js program running on the host specified by the
    {
      "name": "Launch via npm",
      "type": "node",
      "request": "launch",
      "cwd": "${workspaceFolder}",
      "runtimeExecutable": "npm",
      "runtimeArgs": ["run-script", "debug"]
    }
    
    7 attribute. Make sure that the Node.js program to debug has been started in debug mode, and the debug port used is the same as the one specified in the snippet. To help VS Code map source files between your workspace and the filesystem of the remote host, make sure to specify correct paths for the
    "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    2and
    "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    3 attributes.
  • Đính kèm theo ID quy trình: Mở Trình chọn quy trình để chọn một nút hoặc quy trình gulp để gỡ lỗi. Với cấu hình khởi chạy này, bạn thậm chí có thể đính kèm vào một nút hoặc quy trình gulp không bắt đầu ở chế độ gỡ lỗi.: Open the process picker to select a node or gulp process for debugging. With this launch configuration, you can even attach to a node or gulp process that was not started in debug mode.
  • Cài đặt Nodemon: Sử dụng Nodemon để tự động khởi động lại phiên gỡ lỗi bất cứ khi nào nguồn JavaScript đã thay đổi. Hãy chắc chắn rằng bạn đã cài đặt Nodemon trên toàn cầu. Lưu ý rằng việc chấm dứt phiên gỡ lỗi chỉ chấm dứt chương trình để gỡ lỗi, không phải là gật đầu. Để chấm dứt Nodemon, nhấn Ctrl+C trong thiết bị đầu cuối tích hợp.: Use nodemon to relaunch a debug session automatically whenever the JavaScript source has changed. Make sure that you have nodemon installed globally. Note that terminating the debug session only terminates the program to debug, not nodemon itself. To terminate nodemon, press Ctrl+C in the Integrated Terminal.
  • Các bài kiểm tra Mocha: gỡ lỗi các bài kiểm tra mocha trong thư mục
    {
      "type": "node",
      "request": "launch",
      "name": "Launch test",
      "runtimeVersion": "14",
      "program": "${workspaceFolder}/test.js"
    }
    
    8 của dự án của bạn. Đảm bảo rằng dự án của bạn đã cài đặt 'mocha' trong thư mục
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    7 của nó.
    : Debug mocha tests in a
    {
      "type": "node",
      "request": "launch",
      "name": "Launch test",
      "runtimeVersion": "14",
      "program": "${workspaceFolder}/test.js"
    }
    
    8 folder of your project. Make sure that your project has 'mocha' installed in its
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    7 folder.
  • Máy phát điện yeoman: Gỡ lỗi một máy phát điện yeoman. Đoạn trích yêu cầu bạn chỉ định tên của trình tạo. Đảm bảo rằng dự án của bạn đã được cài đặt 'yo' trong thư mục
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    7 và dự án được tạo của bạn đã được cài đặt để gỡ lỗi bằng cách chạy
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    01 trong thư mục dự án.
    : Debug a yeoman generator. The snippet asks you to specify the name of the generator. Make sure that your project has 'yo' installed in its
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    7 folder and that your generated project has been installed for debugging by running
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    01 in the project folder.
  • Nhiệm vụ của Gulp: Gỡ lỗi một nhiệm vụ gulp. Đảm bảo rằng dự án của bạn đã cài đặt 'Gulp' trong thư mục
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    7 của nó.
    : Debug a gulp task. Make sure that your project has 'gulp' installed in its
      "debug.javascript.terminalOptions": {
        "skipFiles": [
          "/**"
        ]
      },
    
    7 folder.
  • Electron Main: Gỡ lỗi Quy trình chính của Node.js của một ứng dụng điện tử. Đoạn trích giả định rằng thực thi điện tử đã được cài đặt bên trong thư mục ____103 của không gian làm việc.: Debug the main Node.js process of an Electron application. The snippet assumes that the Electron executable has been installed inside the
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    03 directory of the workspace.

Bảng điều khiển nút

Theo mặc định, các phiên gỡ lỗi Node.js sẽ khởi chạy mục tiêu trong bảng điều khiển gỡ lỗi nội bộ so với mã. Vì bảng điều khiển gỡ lỗi không hỗ trợ các chương trình cần đọc đầu vào từ bảng điều khiển, bạn có thể bật thiết bị đầu cuối bên ngoài hoặc sử dụng thiết bị đầu cuối tích hợp mã VS bằng cách đặt thuộc tính ____77 trong cấu hình khởi chạy của bạn thành

{
  "name": "Launch via npm",
  "type": "node",
  "request": "launch",
  "cwd": "${workspaceFolder}",
  "runtimeExecutable": "npm",
  "runtimeArgs": ["run-script", "debug"]
}
0 hoặc
  "scripts": {
    "debug": "node myProgram.js"
  },
9. Mặc định là
  "scripts": {
    "debug": "node myProgram.js"
  },
8.

Nếu một thiết bị đầu cuối bên ngoài được sử dụng, bạn có thể định cấu hình chương trình thiết bị đầu cuối nào sẽ sử dụng thông qua cài đặt

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
08,
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
09 và
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
10.

Khởi chạy Hỗ trợ cấu hình cho 'NPM' và các công cụ khác

Thay vì khởi chạy chương trình Node.js trực tiếp với Node, bạn có thể sử dụng các tập lệnh 'NPM' hoặc các công cụ Runner tác vụ khác trực tiếp từ cấu hình khởi chạy:

  • Bất kỳ chương trình nào có sẵn trên đường dẫn (ví dụ 'NPM', 'Mocha', 'Gulp', v.v.) có thể được sử dụng cho thuộc tính
      "scripts": {
        "debug": "node myProgram.js"
      },
    
    1 và các đối số có thể được truyền qua
      "scripts": {
        "debug": "node myProgram.js"
      },
    
    3.
  • Bạn không phải đặt thuộc tính
    "debug.javascript.terminalOptions": {
      "skipFiles": [
        "/**"
      ]
    },
    
    8 nếu tập lệnh NPM hoặc công cụ khác của bạn hoàn toàn chỉ định chương trình để khởi chạy.

Hãy xem xét một ví dụ 'NPM'. Ví dụ: nếu

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
14 của bạn có tập lệnh 'gỡ lỗi', ví dụ:

  "scripts": {
    "debug": "node myProgram.js"
  },

Cấu hình khởi chạy tương ứng sẽ trông như thế này:

{
  "name": "Launch via npm",
  "type": "node",
  "request": "launch",
  "cwd": "${workspaceFolder}",
  "runtimeExecutable": "npm",
  "runtimeArgs": ["run-script", "debug"]
}

Hỗ trợ nhiều phiên bản

Nếu bạn đang sử dụng 'NVM' (hoặc 'NVM-Windows') để quản lý các phiên bản Node.js của bạn, có thể chỉ định thuộc tính

  "scripts": {
    "debug": "node myProgram.js"
  },
4 trong cấu hình khởi chạy để chọn phiên bản cụ thể của Node.js:

{
  "type": "node",
  "request": "launch",
  "name": "Launch test",
  "runtimeVersion": "14",
  "program": "${workspaceFolder}/test.js"
}

Nếu bạn đang sử dụng 'NVS' để quản lý các phiên bản Node.js của mình, có thể sử dụng thuộc tính

  "scripts": {
    "debug": "node myProgram.js"
  },
4 để chọn một phiên bản cụ thể, kiến ​​trúc và hương vị Node.js, ví dụ:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
0

Đảm bảo cài đặt các phiên bản Node.js đó mà bạn muốn sử dụng với thuộc tính

  "scripts": {
    "debug": "node myProgram.js"
  },
4, vì tính năng sẽ không tải xuống và cài đặt phiên bản tự động. Ví dụ: bạn sẽ phải chạy một cái gì đó như
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
18 hoặc
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
19 từ thiết bị đầu cuối tích hợp nếu bạn có kế hoạch thêm
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
20 vào cấu hình khởi chạy của bạn.

Nếu bạn bỏ qua phiên bản nhỏ và bản vá và có, ví dụ,

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
21, thì phiên bản
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
22 gần đây nhất được cài đặt trên hệ thống của bạn sẽ được sử dụng.

Tải các biến môi trường từ tệp bên ngoài

Trình gỡ lỗi nút VS hỗ trợ các biến môi trường tải từ một tệp và chuyển chúng đến thời gian chạy Node.js. Để sử dụng tính năng này, hãy thêm thuộc tính

  "scripts": {
    "debug": "node myProgram.js"
  },
6 vào cấu hình khởi chạy của bạn và chỉ định đường dẫn tuyệt đối vào tệp chứa các biến môi trường:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
1

Bất kỳ biến môi trường nào được chỉ định trong từ điển

  "scripts": {
    "debug": "node myProgram.js"
  },
5 sẽ ghi đè các biến được tải từ tệp.

Dưới đây là một ví dụ về tệp

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
25:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
2

Gắn vào Node.js

Nếu bạn muốn đính kèm Trình gỡ lỗi mã VS vào chương trình Node.js bên ngoài, hãy khởi chạy Node.js như sau:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
3

Hoặc nếu chương trình không nên bắt đầu chạy, nhưng phải chờ trình gỡ lỗi đính kèm:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
4

Bây giờ bạn có một vài tùy chọn để gắn trình gỡ lỗi vào chương trình của mình:

  • Mở một "người chọn quy trình" liệt kê tất cả các quy trình ứng viên tiềm năng và cho phép bạn chọn một hoặc
  • Tạo cấu hình "đính kèm" chỉ định rõ ràng tất cả các tùy chọn cấu hình và sau đó nhấn F5.F5.

Hãy xem qua các tùy chọn này một cách chi tiết:

Đính kèm với hành động quy trình nút

Lệnh đính kèm vào nút Node Process từ bảng lệnh (⇧⌘P (Windows, Linux Ctrl+Shift+P)) mở menu chọn nhanh liệt kê tất cả các quy trình tiềm năng có sẵn cho trình gỡ lỗi Node.js:Attach to Node Process command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) opens a Quick Pick menu that lists all potential processes that are available to the Node.js debugger:

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Các quy trình riêng lẻ được liệt kê trong trình chọn hiển thị cổng gỡ lỗi và ID quy trình. Khi bạn chọn quy trình Node.js của mình trong danh sách đó, trình gỡ lỗi Node.js sẽ cố gắng đính kèm với nó.

Ngoài các quy trình của Node.js, trình chọn cũng hiển thị các chương trình khác được đưa ra với một trong các dạng đối số

[
  '!**/node_modules/**', // exclude scripts in node_modules folders
  '**/$KNOWN_TOOLS$/**' // but include some common tools
];
1 khác nhau. Điều này cho phép gắn vào các quy trình trợ giúp của mã của Electron hoặc VS.

Thiết lập cấu hình "Đính kèm"

Tùy chọn này đòi hỏi nhiều công việc hơn nhưng trái ngược với hai tùy chọn trước đó, nó cho phép bạn định cấu hình các tùy chọn cấu hình gỡ lỗi khác nhau một cách rõ ràng.

Cấu hình "đính kèm" đơn giản nhất trông như thế này:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
5

Cổng

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
27 là cổng gỡ lỗi mặc định của các tùy chọn
[
  '!**/node_modules/**', // exclude scripts in node_modules folders
  '**/$KNOWN_TOOLS$/**' // but include some common tools
];
1 và
[
  '!**/node_modules/**', // exclude scripts in node_modules folders
  '**/$KNOWN_TOOLS$/**' // but include some common tools
];
2. Để sử dụng một cổng khác (ví dụ
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
30), hãy thêm nó vào các tùy chọn như sau:
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
31 và
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
32 và thay đổi thuộc tính
{
  "name": "Launch via npm",
  "type": "node",
  "request": "launch",
  "cwd": "${workspaceFolder}",
  "runtimeExecutable": "npm",
  "runtimeArgs": ["run-script", "debug"]
}
6 trong cấu hình khởi chạy để khớp.

Nếu bạn muốn đính kèm vào quy trình Node.js chưa được bắt đầu ở chế độ gỡ lỗi, bạn có thể thực hiện điều này bằng cách chỉ định ID quy trình của quy trình Node.js dưới dạng chuỗi:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
6

Vì có một chút tốn nhiều công sức khi liên tục tìm ID quy trình và nhập nó vào cấu hình khởi chạy, Node Debug hỗ trợ biến lệnh

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
34 sẽ mở trình chọn quy trình (từ phía trên).

Sử dụng biến

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
34 Cấu hình khởi chạy trông như thế này:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
7

Ngừng gỡ lỗi

Sử dụng Debug: Stop Action (có sẵn trong thanh công cụ gỡ lỗi hoặc thông qua bảng lệnh) dừng phiên gỡ lỗi.Debug: Stop action (available in the Debug toolbar or via the Command Palette) stops the debug session.

Nếu phiên gỡ lỗi được bắt đầu ở chế độ "đính kèm" (và nút chấm dứt màu đỏ trong thanh công cụ gỡ lỗi hiển thị "phích cắm" được đặt chồng lên nhau), hãy nhấn dừng sẽ ngắt kết nối trình gỡ lỗi Node.js từ trình gỡ lỗi sau đó tiếp tục thực thi.Stop disconnects the Node.js debugger from the debuggee that then continues execution.

Nếu phiên gỡ lỗi ở chế độ "Khởi chạy", hãy nhấn Stop như sau:Stop does the following:

  1. Khi nhấn dừng lần đầu tiên, bản gỡ lỗi được yêu cầu tắt một cách duyên dáng bằng cách gửi tín hiệu

    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    36. Người gỡ lỗi có thể tự do chặn tín hiệu này và làm sạch bất cứ thứ gì khi cần thiết và sau đó tắt. Nếu không có điểm dừng (hoặc vấn đề) trong mã tắt đó, phiên bản gỡ lỗi và phiên gỡ lỗi sẽ chấm dứt.Stop for the first time, the debuggee is requested to shut down gracefully by sending a
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    36 signal. The debuggee is free to intercept this signal and clean up anything as necessary and then shut down. If there are no breakpoints (or problems) in that shutdown code, the debuggee and the debug session will terminate.

  2. Tuy nhiên, nếu trình gỡ lỗi đạt điểm dừng trong mã tắt hoặc nếu bản gỡ lỗi không tự chấm dứt đúng cách, thì phiên gỡ lỗi sẽ không kết thúc. Trong trường hợp này, nhấn dừng lại sẽ buộc chấm dứt gỡ lỗi và các quy trình con của nó (

    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    37).Stop again will force terminate the debuggee and its child processes (
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    37).

Vì vậy, nếu bạn thấy rằng một phiên gỡ lỗi không kết thúc khi bạn nhấn nút Dừng màu đỏ, thì hãy nhấn nút một lần nữa để buộc tắt máy gỡ lỗi.Stop button, then press the button again to force a shutdown of the debuggee.

Lưu ý rằng trên hệ điều hành Windows, nhấn dừng luôn luôn giết chết các quy trình gỡ lỗi và con của nó.Stop always forcibly kills the debuggee and its child processes.

Bản đồ nguồn

Trình gỡ lỗi JavaScript của mã VS hỗ trợ các bản đồ nguồn giúp gỡ lỗi các ngôn ngữ được chuyển đổi, ví dụ, JavaScript hoặc Minified/Uglified. Với bản đồ nguồn, có thể một bước đơn qua hoặc đặt các điểm dừng trong nguồn gốc. Nếu không có bản đồ nguồn nào tồn tại cho nguồn gốc hoặc nếu bản đồ nguồn bị phá vỡ và không thể lập bản đồ thành công giữa nguồn và JavaScript được tạo, thì các điểm dừng hiển thị dưới dạng chưa được xác minh (vòng tròn rỗng màu xám).

Tính năng bản đồ nguồn được điều khiển bởi thuộc tính

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
38 mặc định là
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
39. Điều này có nghĩa là trình gỡ lỗi luôn cố gắng sử dụng bản đồ nguồn (nếu nó có thể tìm thấy bất kỳ) và do đó, bạn thậm chí có thể chỉ định một tệp nguồn (ví dụ: app.ts) với thuộc tính
"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
8. Nếu bạn cần vô hiệu hóa bản đồ nguồn vì một số lý do, bạn có thể đặt thuộc tính
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
38 thành
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
42.

Cấu hình công cụ

Vì bản đồ nguồn không phải lúc nào cũng tự động được tạo, bạn nên đảm bảo định cấu hình bộ chuyển đổi của mình để tạo chúng. Ví dụ:

TYPEXTRIPT

Đối với TypeScript, bạn có thể bật SourCemaps bằng cách chuyển

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
43 đến
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
44 hoặc bằng cách thêm
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
45 trong tệp tsconfig.json của bạn.

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
8

Babel

Đối với Babel, bạn sẽ muốn đặt tùy chọn SourCemaps thành

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
39 hoặc chuyển tùy chọn
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
47 khi biên dịch mã của bạn.

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
9

Trang web

Webpack có nhiều tùy chọn bản đồ nguồn. Chúng tôi khuyên bạn nên đặt tài sản

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
48 trong
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
49 của bạn để biết độ trung thực tốt nhất về kết quả, mặc dù bạn có thể thử nghiệm các cài đặt khác gây ra sự chậm lại trong bản dựng của bạn.

Ngoài ra, nếu bạn có các bước biên dịch bổ sung trong trang web, chẳng hạn như sử dụng Trình tải TypeScript, bạn cũng sẽ muốn đảm bảo rằng các bước đó được thiết lập để tạo SourCemaps. Mặt khác, các bản đồ mà webpack tạo sẽ ánh xạ trở lại mã được biên dịch từ trình tải, thay vì các nguồn thực.

Khám phá bản đồ nguồn

Theo mặc định, mã VS sẽ tìm kiếm toàn bộ không gian làm việc của bạn, không bao gồm

  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
7, cho SourCemaps. Trong không gian làm việc lớn, tìm kiếm này có thể chậm. Bạn có thể định cấu hình các vị trí nơi mã VS sẽ tìm kiếm bản đồ nguồn bằng cách đặt thuộc tính
[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
8 trong
  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
1 của bạn. Ví dụ: cấu hình này sẽ chỉ phát hiện ra SourCemaps cho các tệp
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
53 trong thư mục
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
54:

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
0

Lưu ý rằng

[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
8 sẽ khớp với các tệp JavaScript của bạn, chứ không phải các tệp bản đồ nguồn (có thể kết thúc trong
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
56 thay vì
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
53).

Độ phân giải bản đồ nguồn

Theo mặc định, chỉ các bản đồ nguồn trong

[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
8 của bạn sẽ được giải quyết. Hành vi này được sử dụng để ngăn chặn các phụ thuộc can thiệp vào các điểm dừng bạn đặt. Ví dụ: nếu bạn có tệp
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
59 và phụ thuộc có bản đồ nguồn được tham chiếu
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
60, điều đó sẽ giải quyết không chính xác cho tệp nguồn của bạn và có thể dẫn đến kết quả đáng ngạc nhiên.

Bạn có thể định cấu hình hành vi này bằng cách đặt tùy chọn

[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
9. Nếu được đặt thành
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
62, mọi bản đồ nguồn sẽ được giải quyết. Ví dụ, cấu hình này cũng sẽ cho phép bản đồ nguồn trong
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
63 được giải quyết:

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
1

Bước thông minh

Với thuộc tính

"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
4 được đặt thành
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
39 trong cấu hình khởi chạy, mã VS sẽ tự động bỏ qua 'mã không trung tâm' khi bước qua mã trong trình gỡ lỗi. 'Mã không thú vị' là mã được tạo bởi một quy trình transpiling nhưng không được bao phủ bởi bản đồ nguồn để nó không ánh xạ trở lại nguồn gốc. Mã này theo cách của bạn khi bước qua mã nguồn trong trình gỡ lỗi vì nó tạo công tắc trình gỡ lỗi giữa mã nguồn gốc và mã được tạo mà bạn không quan tâm. Một vị trí được bao phủ bởi một bản đồ nguồn một lần nữa.

Bước thông minh đặc biệt hữu ích cho các trường hợp như Async/Await DownCompilation trong TypeScript, trong đó trình biên dịch tiêm mã trợ giúp không được bao phủ bởi bản đồ nguồn.

Tính năng

"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
4 chỉ áp dụng cho mã JavaScript được tạo từ nguồn và do đó có bản đồ nguồn. Đối với JavaScript không có nguồn, tùy chọn bước thông minh không có hiệu lực.

Mẹo bản đồ nguồn JavaScript

Một vấn đề phổ biến khi gỡ lỗi với các bản đồ nguồn là bạn sẽ đặt điểm dừng và nó sẽ chuyển sang màu xám. Nếu bạn di chuyển con trỏ qua nó, bạn sẽ thấy tin nhắn,

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
68. Gì bây giờ? Có một loạt các vấn đề có thể dẫn đến điều này. Đầu tiên, một lời giải thích nhanh về cách bộ điều hợp gỡ lỗi nút xử lý các bản đồ nguồn.

Khi bạn đặt điểm dừng trong

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
69, bộ điều hợp gỡ lỗi phải tìm ra đường dẫn đến
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
70, phiên bản được truyền của tệp TypeScript của bạn, đó là những gì thực sự đang chạy trong nút. Nhưng, không có cách nào đơn giản để tìm ra điều này bắt đầu từ tệp
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
71. Thay vào đó, bộ điều hợp gỡ lỗi sử dụng thuộc tính
[
  '!**/node_modules/**',
  '**/$KNOWN_TOOLS$/**',
  '!**/node_modules/mocha/**', // use "!" to exclude all scripts in "mocha" node modules
  '**/node_modules/my-cool-test-runner/**' // include scripts in the custom test runner
];
8 trong
  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
1 để tìm tất cả các tệp
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
53 đã được truyền và phân tích chúng cho một bản đồ nguồn, chứa các vị trí của các tệp
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
71 được liên kết của nó.

Khi bạn xây dựng tệp

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
69 của mình trong TypeScript với các bản đồ nguồn được bật, nó sẽ tạo ra một tệp
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
77 hoặc bản đồ nguồn được ghi lại dưới dạng chuỗi được mã hóa cơ sở64 trong nhận xét ở dưới cùng của tệp
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
70. Để tìm các tệp
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
71 được liên kết với bản đồ này, bộ điều hợp gỡ lỗi xem xét hai thuộc tính trong bản đồ nguồn,
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
80 và
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
81.
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
81 là tùy chọn - nếu có, nó được chuẩn bị cho mỗi đường dẫn trong
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
80, đây là một loạt các đường dẫn. Kết quả là một mảng các đường dẫn tuyệt đối hoặc tương đối đến các tệp
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
71. Các đường dẫn tương đối được giải quyết liên quan đến bản đồ nguồn.

Cuối cùng, bộ điều hợp gỡ lỗi tìm kiếm đường dẫn đầy đủ của

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
69 trong danh sách kết quả này của các tệp
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
71. Nếu có một trận đấu, nó đã tìm thấy tệp bản đồ nguồn để sử dụng khi ánh xạ
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
69 đến
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
70. Nếu không có khớp, thì nó không thể liên kết điểm dừng và nó sẽ chuyển sang màu xám.

Dưới đây là một số điều cần thử khi điểm dừng của bạn chuyển sang màu xám:

  • Trong khi gỡ lỗi, hãy chạy lệnh gỡ lỗi: Chẩn đoán các vấn đề điểm dừng. Lệnh này sẽ đưa ra một công cụ có thể cung cấp các gợi ý để giúp bạn giải quyết mọi vấn đề từ bảng lệnh (⇧⌘P (Windows, Linux Ctrl+Shift+P)).Debug: Diagnose Breakpoint Problems command. This command will bring up a tool that can provide hints to help you resolve any issues from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).
  • Bạn đã xây dựng với các bản đồ nguồn được bật chưa? Đảm bảo có các tệp
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    89 hoặc bản đồ nguồn được in trong các tệp
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    53 của bạn.
  • Các thuộc tính
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    81 và
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    80 có đúng trong bản đồ nguồn của bạn không? Chúng có thể được kết hợp để có được đường dẫn chính xác đến tệp
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    71 không?
  • Bạn đã mở thư mục trong mã VS với trường hợp không chính xác chưa? Có thể mở thư mục
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    94 từ dòng lệnh như
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    95 trong trường hợp đó, bản đồ nguồn có thể không được giải quyết chính xác.
  • Hãy thử tìm kiếm trợ giúp với thiết lập cụ thể của bạn trên Stack Overflow hoặc bằng cách nộp một vấn đề trên GitHub.
  • Hãy thử thêm một câu lệnh
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    96. Nếu nó đột nhập vào tệp
    const express = require('express')
    const app = express()
    const port = 3000
    
    app.get('/', (req, res) => {
      const message = 'Hello World'
      res.send(message)
    })
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`)
    })
    
    71 ở đó, nhưng các điểm dừng tại điểm đó không ràng buộc, đó là thông tin hữu ích để bao gồm một vấn đề GitHub.

Gỡ lỗi từ xa

Lưu ý: Mã VS hiện có khả năng phát triển từ xa toàn cầu. Sử dụng các tiện ích mở rộng phát triển từ xa, phát triển Node.js trong các kịch bản và container từ xa không khác gì phát triển Node.js trong một thiết lập cục bộ. Đây là cách được đề xuất để các chương trình Debug Node.js từ xa. Kiểm tra phần Bắt đầu và hướng dẫn từ xa để tìm hiểu thêm. VS Code now has universal remote development capabilities. Using the Remote Development extensions, Node.js development in remote scenarios and containers is no different than Node.js development in a local setup. This is the recommended way to remote debug Node.js programs. Check out the Getting started section and Remote tutorials to learn more.

Nếu bạn không thể sử dụng bất kỳ tiện ích mở rộng phát triển từ xa nào để gỡ lỗi chương trình Node.js của bạn, bên dưới là hướng dẫn về cách gỡ lỗi chương trình Node.js từ xa từ phiên bản VS của bạn.

Trình gỡ lỗi Node.js hỗ trợ gỡ lỗi từ xa nơi bạn gắn vào một quy trình chạy trên một máy khác hoặc trong một thùng chứa. Chỉ định một máy chủ từ xa thông qua thuộc tính

{
  "name": "Launch via npm",
  "type": "node",
  "request": "launch",
  "cwd": "${workspaceFolder}",
  "runtimeExecutable": "npm",
  "runtimeArgs": ["run-script", "debug"]
}
7. Ví dụ:

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
2

Theo mặc định, mã VS sẽ truyền phát nguồn được gỡ lỗi từ thư mục Node.js từ xa đến mã VS cục bộ và hiển thị nó trong trình chỉnh sửa chỉ đọc. Bạn có thể bước qua mã này, nhưng không thể sửa đổi nó. Nếu bạn muốn mã VS mở nguồn có thể chỉnh sửa từ không gian làm việc của mình, bạn có thể thiết lập ánh xạ giữa các vị trí từ xa và địa phương. Thuộc tính

"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
2 và
"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
3 có thể được sử dụng để ánh xạ các đường dẫn giữa dự án mã VS cục bộ và thư mục Node.js của Node.js. Điều này hoạt động ngay cả cục bộ trên cùng một hệ thống hoặc trên các hệ điều hành khác nhau. Bất cứ khi nào một đường dẫn mã cần được chuyển đổi từ thư mục Node.js từ xa sang đường dẫn mã VS cục bộ, đường dẫn
"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
3 bị loại bỏ đường dẫn và được thay thế bằng
"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
2. Đối với chuyển đổi ngược, đường dẫn
"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
2 được thay thế bằng
"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
3.

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
3

Truy cập các tập lệnh được tải

Nếu bạn cần đặt điểm dừng trong tập lệnh không phải là một phần của không gian làm việc của bạn và do đó không thể dễ dàng xác định và mở qua duyệt tệp mã vs thông thường, bạn có thể truy cập các tập lệnh được tải thông qua chế độ xem tập lệnh được tải trong chế độ xem chạy và gỡ lỗi:LOADED SCRIPTS view in the Run and Debug view:

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Chế độ xem tập lệnh được tải cho phép bạn nhanh chóng chọn tập lệnh bằng cách nhập tên của nó hoặc lọc danh sách khi bật bộ lọc trên loại.LOADED SCRIPTS view lets you quickly select the script by typing its name or filter the list when Enable Filter on Type is on.

Các tập lệnh được tải vào một trình soạn thảo chỉ đọc nơi bạn có thể đặt các điểm dừng. Các điểm dừng này được ghi nhớ trong các phiên gỡ lỗi nhưng bạn chỉ có quyền truy cập vào nội dung tập lệnh trong khi phiên gỡ lỗi đang chạy.

Khởi động lại các phiên gỡ lỗi tự động khi nguồn được chỉnh sửa

Thuộc tính

{
  "name": "Launch via npm",
  "type": "node",
  "request": "launch",
  "cwd": "${workspaceFolder}",
  "runtimeExecutable": "npm",
  "runtimeArgs": ["run-script", "debug"]
}
5 của cấu hình khởi chạy có kiểm soát xem bộ gỡ lỗi Node.js sẽ tự động khởi động lại sau khi phiên gỡ lỗi đã kết thúc hay không. Tính năng này rất hữu ích nếu bạn sử dụng Nodemon để khởi động lại Node.js trên các thay đổi tệp. Đặt thuộc tính cấu hình khởi chạy
{
  "name": "Launch via npm",
  "type": "node",
  "request": "launch",
  "cwd": "${workspaceFolder}",
  "runtimeExecutable": "npm",
  "runtimeArgs": ["run-script", "debug"]
}
5 thành
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
39 làm cho trình gỡ lỗi nút tự động cố gắng gắn lại đến Node.js sau khi Node.js chấm dứt.nodemon to restart Node.js on file changes. Setting the launch configuration attribute
{
  "name": "Launch via npm",
  "type": "node",
  "request": "launch",
  "cwd": "${workspaceFolder}",
  "runtimeExecutable": "npm",
  "runtimeArgs": ["run-script", "debug"]
}
5 to
const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
39 makes the node debugger automatically try to reattach to Node.js after Node.js has terminated.

Nếu bạn đã bắt đầu chương trình của mình

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
08 qua Nodemon trên dòng lệnh như sau:nodemon on the command line like this:

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
4

Bạn có thể đính kèm Trình gỡ lỗi mã VS với cấu hình khởi chạy sau:

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
5

Ngoài ra, bạn có thể bắt đầu chương trình của mình

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
08 qua Nodemon trực tiếp với cấu hình khởi chạy và đính kèm trình gỡ lỗi mã VS:nodemon directly with a launch config and attach the VS Code debugger:

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
6

Mẹo: Nhấn nút dừng dừng phiên gỡ lỗi và ngắt kết nối khỏi Node.js, nhưng Nodemon (và Node.js) sẽ tiếp tục chạy. Để dừng Nodemon, bạn sẽ phải giết nó từ dòng lệnh (điều này dễ dàng có thể nếu bạn sử dụng

  "scripts": {
    "debug": "node myProgram.js"
  },
9 như được hiển thị ở trên). Pressing the Stop button stops the debug session and disconnects from Node.js, but nodemon (and Node.js) will continue to run. To stop nodemon, you will have to kill it from the command line (which is easily possible if you use the
  "scripts": {
    "debug": "node myProgram.js"
  },
9 as shown above).

Mẹo: Trong trường hợp lỗi cú pháp, Nodemon sẽ không thể bắt đầu Node.js thành công cho đến khi lỗi đã được sửa. Trong trường hợp này, mã VS sẽ tiếp tục cố gắng đính kèm vào Node.js nhưng cuối cùng bỏ cuộc (sau 10 giây). Để tránh điều này, bạn có thể tăng thời gian chờ bằng cách thêm thuộc tính

"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
0 có giá trị lớn hơn (tính bằng mili giây). In case of syntax errors, nodemon will not be able to start Node.js successfully until the error has been fixed. In this case, VS Code will continue trying to attach to Node.js but eventually give up (after 10 seconds). To avoid this, you can increase the timeout by adding a
"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
0 attribute with a larger value (in milliseconds).

Khởi động lại khung

Trình gỡ lỗi nút hỗ trợ khởi động lại thực thi tại khung ngăn xếp. Điều này có thể hữu ích trong các tình huống mà bạn đã tìm thấy một vấn đề trong mã nguồn của mình và bạn muốn chạy lại một phần nhỏ của mã với các giá trị đầu vào đã sửa đổi. Dừng lại và sau đó khởi động lại phiên gỡ lỗi đầy đủ có thể tốn thời gian. Hành động khung khởi động lại cho phép bạn nhập lại chức năng hiện tại sau khi bạn đã thay đổi các biến với hành động giá trị đã đặt:Restart Frame action allows you to reenter the current function after you have changed variables with the Set Value action:

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Khởi động lại khung sẽ không quay trở lại đột biến để trạng thái bên ngoài chức năng, vì vậy nó có thể không phải lúc nào cũng hoạt động như mong đợi. won't roll back mutation to state outside of the function, so it may not always work as expected.

Điểm dừng

Điểm dừng có điều kiện

Các điểm dừng có điều kiện là các điểm dừng chỉ tạm dừng khi một biểu thức trả về giá trị sự thật. Bạn có thể tạo một bằng cách nhấp chuột phải vào máng xối bên cạnh số dòng và chọn "điểm dừng có điều kiện":

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Các điểm logpi

Đôi khi bạn muốn chỉ đăng nhập một tin nhắn hoặc giá trị khi mã chạm vào một vị trí nhất định, thay vì tạm dừng. Bạn có thể làm điều này với các logpoints. Các điểm logpi không tạm dừng, mà là đăng nhập một thông báo vào bảng điều khiển gỡ lỗi khi bị truy cập. Trong trình gỡ lỗi JavaScript, bạn có thể sử dụng niềng răng xoăn để nội suy các biểu thức vào thông điệp, như

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
12.

Bạn có thể tạo một bằng cách nhấp chuột phải vào máng xối bên cạnh số dòng và chọn "logpoint". Ví dụ: điều này có thể đăng nhập một cái gì đó như

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
13:

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Hit Bount Breakpoints

Điều kiện 'Điều kiện đếm trúng' kiểm soát số lần một điểm dừng cần phải được nhấn trước khi thực hiện 'phá vỡ'. Bạn có thể đặt điểm dừng đếm truy cập bằng cách nhấp chuột phải vào máng xối bên cạnh một số dòng, chọn "điểm dừng có điều kiện", sau đó chuyển sang "nhấn đếm".

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Cú pháp đếm trúng được hỗ trợ bởi Trình gỡ lỗi Node.js là một số nguyên hoặc một trong các toán tử

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
14,
{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
15,
{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
16,
{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
17,
{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
18,
{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
19 theo sau là một số nguyên.

Vài ví dụ:

  • {
      // 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": "pwa-node",
          "request": "launch",
          "name": "Launch Program",
          "skipFiles": [
            "/**"
          ],
          "program": "${workspaceFolder}\\index.js"
        }
      ]
    }
    
    20 LUÔN LUÔN sau 10 lần truy cập
  • {
      // 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": "pwa-node",
          "request": "launch",
          "name": "Launch Program",
          "skipFiles": [
            "/**"
          ],
          "program": "${workspaceFolder}\\index.js"
        }
      ]
    }
    
    21 chỉ phá vỡ trong hai lần truy cập đầu tiên
  • {
      // 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": "pwa-node",
          "request": "launch",
          "name": "Launch Program",
          "skipFiles": [
            "/**"
          ],
          "program": "${workspaceFolder}\\index.js"
        }
      ]
    }
    
    22 giống như
    {
      // 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": "pwa-node",
          "request": "launch",
          "name": "Launch Program",
          "skipFiles": [
            "/**"
          ],
          "program": "${workspaceFolder}\\index.js"
        }
      ]
    }
    
    23
  • {
      // 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": "pwa-node",
          "request": "launch",
          "name": "Launch Program",
          "skipFiles": [
            "/**"
          ],
          "program": "${workspaceFolder}\\index.js"
        }
      ]
    }
    
    24 phá vỡ mọi cú đánh khác

Xác thực điểm dừng

Vì lý do hiệu suất, Node.js phân tích các chức năng bên trong các tệp JavaScript một cách uể oải trên quyền truy cập đầu tiên. Do đó, các điểm dừng không hoạt động trong các khu vực mã nguồn chưa được nhìn thấy (phân tích cú pháp) bởi Node.js.

Vì hành vi này không lý tưởng để gỡ lỗi, mã VS sẽ tự động chuyển tùy chọn

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
25 cho Node.js. Điều này ngăn chặn phân tích cú pháp bị trì hoãn và đảm bảo rằng các điểm dừng có thể được xác thực trước khi chạy mã (để chúng không còn "nhảy").

Vì tùy chọn

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
25 có thể tăng đáng kể thời gian khởi động của mục tiêu gỡ lỗi, bạn có thể dễ dàng từ chối bằng cách truyền một thuộc tính
{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
27 dưới dạng
  "scripts": {
    "debug": "node myProgram.js"
  },
3.

Khi làm như vậy, bạn sẽ thấy rằng một số điểm dừng của bạn không "gắn bó" vào dòng được yêu cầu mà thay vào đó "nhảy" cho dòng tiếp theo có thể trong mã đã bị lỗi. Để tránh nhầm lẫn, mã VS luôn hiển thị các điểm dừng tại vị trí nơi Node.js nghĩ rằng điểm dừng là. Trong phần Breakpoints, các điểm dừng này được hiển thị với một mũi tên giữa số dòng được yêu cầu và thực tế:BREAKPOINTS section, these breakpoints are shown with an arrow between requested and actual line number:

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Xác thực điểm dừng này xảy ra khi một phiên bắt đầu và các điểm dừng được đăng ký với Node.js hoặc khi một phiên đã chạy và một điểm dừng mới được đặt. Trong trường hợp này, điểm dừng có thể "nhảy" đến một vị trí khác. Sau khi Node.js đã phân tích tất cả các mã (ví dụ: bằng cách chạy qua nó), các điểm dừng có thể dễ dàng áp dụng lại cho các vị trí được yêu cầu bằng nút áp dụng lại trong tiêu đề phần Breakpoints. Điều này sẽ làm cho các điểm dừng "nhảy trở lại" đến vị trí được yêu cầu.Reapply button in the BREAKPOINTS section header. This should make the breakpoints "jump back" to the requested location.

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Bỏ qua mã không thú vị

Vs Code Node.js Debugging có một tính năng để tránh mã nguồn mà bạn không muốn bước qua (còn được gọi là 'Just My Code'). Tính năng này có thể được bật với thuộc tính

"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
5 trong cấu hình khởi chạy của bạn.
"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
5 là một mảng các mẫu GLOB cho các đường dẫn tập lệnh bỏ qua.

Ví dụ: sử dụng:

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
7

Tất cả mã trong các thư mục

  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
7 và
{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
32 trong dự án của bạn sẽ bị bỏ qua.
"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
5 cũng áp dụng cho vị trí được hiển thị khi gọi
{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
34 và các phương thức tương tự: vị trí không bị trượt đầu tiên trong ngăn xếp sẽ được hiển thị bên cạnh đầu ra trong bảng điều khiển gỡ lỗi.

Các mô-đun lõi tích hợp của Node.js có thể được gọi bằng 'Tên ma thuật'

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
35 trong một mẫu GLOB. Ví dụ sau bỏ qua tất cả các mô -đun nội bộ:core modules of Node.js can be referred to by the 'magic name'
{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
35 in a glob pattern. The following example skips all internal modules:

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
8

Các quy tắc 'bỏ qua' chính xác như sau:

  • Nếu bạn bước vào một tệp bị bỏ qua, bạn sẽ không dừng ở đó - bạn sẽ dừng trên dòng thực hiện tiếp theo không có trong tệp bị bỏ qua.
  • Nếu bạn đã đặt tùy chọn để phá vỡ các ngoại lệ, thì bạn sẽ không phá vỡ các ngoại lệ được ném từ các tệp bị bỏ qua trừ khi chúng bong bóng vào một tệp không bị bắt.
  • Nếu bạn đặt điểm dừng trong một tệp bị bỏ qua, bạn sẽ dừng ở điểm dừng đó và bạn sẽ có thể bước qua nó cho đến khi bạn bước ra khỏi nó, tại đó hành vi bỏ qua bình thường sẽ tiếp tục.
  • Vị trí của các thông báo console từ bên trong các tệp bỏ qua sẽ được hiển thị dưới dạng vị trí không bị bắt đầu đầu tiên trong ngăn xếp cuộc gọi.

Nguồn bị bỏ qua được hiển thị theo kiểu 'mờ' trong chế độ xem ngăn xếp cuộc gọi:

Hướng dẫn debug nodejs vscode - gỡ lỗi nodejs vscode

Di chuột qua các mục mờ giải thích lý do tại sao khung ngăn xếp bị mờ.

Một mục menu ngữ cảnh trên ngăn xếp cuộc gọi, chuyển đổi bỏ qua tệp này cho phép bạn dễ dàng bỏ qua một tệp trong thời gian chạy mà không cần thêm nó vào cấu hình khởi chạy của bạn. Tùy chọn này chỉ tồn tại cho phiên gỡ lỗi hiện tại. Bạn cũng có thể sử dụng nó để ngừng bỏ qua một tệp bị bỏ qua bởi tùy chọn

"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
5 trong cấu hình khởi chạy của bạn.Toggle skipping this file enables you to easily skip a file at runtime without adding it to your launch config. This option only persists for the current debugging session. You can also use it to stop skipping a file that is skipped by the
"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
5 option in your launch config.

Lưu ý: Trình gỡ lỗi giao thức ____237 hỗ trợ các mẫu GLOM âm, nhưng chúng phải tuân theo một mẫu tích cực: Các mẫu tích cực thêm vào tập hợp các tệp bị bỏ qua, trong khi các mẫu âm trừ vào tập hợp đó. The

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
37 protocol debugger supports negative glob patterns, but they must follow a positive pattern: positive patterns add to the set of skipped files, while negative patterns subtract from that set.

Trong ví dụ (chỉ dành cho giao thức

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
37) Ví dụ, tất cả trừ một mô-đun 'toán' bị bỏ qua:

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
9

Lưu ý: Trình gỡ lỗi giao thức ____237 phải mô phỏng tính năng

"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
5 vì giao thức trình gỡ lỗi V8 không hỗ trợ nó. Điều này có thể dẫn đến hiệu suất bước chậm. The
{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
37 protocol debugger has to emulate the
"debug.javascript.terminalOptions": {
  "skipFiles": [
    "/**"
  ]
},
5 feature because the V8 Debugger Protocol does not support it natively. This might result in slow stepping performance.

Hỗ trợ Runtimes giống như nút

Trình gỡ lỗi JavaScript hiện tại của mã hỗ trợ phiên bản nút ở hoặc trên 8.x, các phiên bản Chrome gần đây và các phiên bản cạnh gần đây (thông qua loại khởi chạy

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
41).

Bước tiếp theo

Trong trường hợp bạn chưa đọc phần Node.js, hãy xem:

  • Node.js - Kịch bản nút từ đầu đến cuối với ứng dụng mẫu

Để xem các hướng dẫn về những điều cơ bản của Node.js gỡ lỗi, hãy xem các video này:

  • Video giới thiệu - Debugging - Video giới thiệu giới thiệu những điều cơ bản của việc gỡ lỗi.
  • Bắt đầu với gỡ lỗi Node.js - Tìm hiểu cách gắn vào quy trình Running Node.js.

Để tìm hiểu về hỗ trợ chạy nhiệm vụ của mã vs, hãy truy cập:

  • Nhiệm vụ - Nhiệm vụ chạy với Gulp, Grunt và Jake. Hiển thị lỗi và cảnh báo

Để viết tiện ích mở rộng trình gỡ lỗi của riêng bạn, hãy truy cập:

  • Tiện ích mở rộng trình gỡ lỗi - Các bước để tạo tiện ích mở rộng gỡ lỗi mã vs bắt đầu từ mẫu giả

Câu hỏi thường gặp

Có, nếu bạn đã tạo các liên kết symlink cho các thư mục bên trong dự án của mình, chẳng hạn như với

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
  const message = 'Hello World'
  res.send(message)
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})
01, bạn có thể gỡ lỗi các nguồn được liên kết với nhau bằng cách nói thời gian chạy của Node.js để bảo tồn các đường dẫn được liên kết với nhau. Sử dụng công tắc Node.exe
{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
43 trong thuộc tính cấu hình khởi chạy của bạn
  "scripts": {
    "debug": "node myProgram.js"
  },
3.
  "scripts": {
    "debug": "node myProgram.js"
  },
3, một loạt các chuỗi, được chuyển đến phiên chạy thời gian chạy gỡ lỗi có thể thực thi, mặc định là Node.exe.

  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
0

Nếu tập lệnh chính của bạn nằm trong một đường dẫn được liên kết với nhau, thì bạn cũng sẽ cần thêm tùy chọn

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
46. Tùy chọn này chỉ có sẵn trong nút 10+.

Làm cách nào để gỡ lỗi các mô -đun Ecmascript?

Nếu bạn sử dụng ESM hoặc chuyển

{
  // 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": "pwa-node",
      "request": "launch",
      "name": "Launch Program",
      "skipFiles": [
        "/**"
      ],
      "program": "${workspaceFolder}\\index.js"
    }
  ]
}
47 cho Node.js để sử dụng các mô -đun ECMAscript, bạn có thể chuyển các tùy chọn này thông qua thuộc tính
  "scripts": {
    "debug": "node myProgram.js"
  },
3 của
  "debug.javascript.terminalOptions": {
    "skipFiles": [
      "/**"
    ]
  },
1:

  • {
      // 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": "pwa-node",
          "request": "launch",
          "name": "Launch Program",
          "skipFiles": [
            "/**"
          ],
          "program": "${workspaceFolder}\\index.js"
        }
      ]
    }
    
    50 - Sử dụng hỗ trợ mô -đun ECMAScript thử nghiệm trong nút V8.5.0+
  • {
      // 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": "pwa-node",
          "request": "launch",
          "name": "Launch Program",
          "skipFiles": [
            "/**"
          ],
          "program": "${workspaceFolder}\\index.js"
        }
      ]
    }
    
    51 - Sử dụng Trình tải mô -đun ESM ES (
    {
      // 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": "pwa-node",
          "request": "launch",
          "name": "Launch Program",
          "skipFiles": [
            "/**"
          ],
          "program": "${workspaceFolder}\\index.js"
        }
      ]
    }
    
    52 mà không có dấu phẩy sẽ không hoạt động)

11/2/2022