node.js - vscode 無法啟動 node 應用程式
我正在嘗試使用 Visual Studio Code 和 gulp 設置一個基於 TypeScript 的 Express 應用程序工作流程。
這是我的專案結構:
src/ <-- souce files
Start.ts
Server.ts
models/
Contact.ts
Organization.ts
bin/ <-- compiled output
Start.js
Start.js.map
...
tsconfig.json
gulpfile.json
package.json
.vscode/
launch.json
執行以下指令序列,我可以在整合終端機中編譯並啟動我的應用程式:
> tsc
> node --debug-brk ./bin/Start.js
在這個階段,我可以成功使用預設的「附加到進程」命令附加到我的應用程式(甚至可以正確命中 TypeScript 文件中的斷點,耶!):
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"address": "localhost",
"port": 5858
}
然而,每次用 F5 啟動都會失敗。在除錯主控台上沒有任何輸出,幾秒鐘後我會在頂部看到一個錯誤橫幅,上面寫著 無法通過 'legacy' 協議連接到運行時;請考慮使用 'inspector' 協議(10,000 毫秒後超時)。
這是我的啟動配置(在 launch.json 中):
{
"type": "node",
"request": "launch",
"name": "Launch Program",
// Using compiled .js file. vscode should use the sourcemap to correlate
// with breakpoints in the source file
"program": "${workspaceRoot}/bin/Start.js",
"outFiles": [ "${workspaceRoot}/bin/**/*.js" ]
}
我嘗試打開除錯控制台。每次我保存 launch.json 文件時,它都會給我以下錯誤:Cannot read property '_runner' of undefined: TypeError: Cannot read property '_runner' of undefined 在 shell.ts:426
在谷歌搜尋這個錯誤時,我遇到了這個漏洞
這個錯誤是什麼意思?有什麼解決方法嗎?我該怎麼辦???
答案#1
小事情總是引起最大的問題。
問題在於我選擇了「附加到進程」任務(在除錯模式下拉選單中),而不是「啟動程式」任務。因此,當我按下 F5 時,vscode 嘗試附加到已經運行的進程,而不是啟動新的進程。
**拍額**
答案#2
我無法告訴你這個錯誤的意思,但下面我將提供一個在與你類似環境中對我有效的啟動配置範例:
{
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Launch Server",
// Type of configuration.
"type": "node2",
// Workspace relative or absolute path to the program.
"program": "${workspaceRoot}/server/app.ts",
// Automatically stop program after launch.
"stopOnEntry": false,
// Command line arguments passed to the program.
"args": [],
// Workspace relative or absolute path to the working directory of the program being debugged. Default is the current workspace.
"cwd": "${workspaceRoot}",
// Workspace relative or absolute path to the runtime executable to be used. Default is the runtime executable on the PATH.
"runtimeExecutable": null,
// Optional arguments passed to the runtime executable.
"runtimeArgs": ["--nolazy"],
// Environment variables passed to the program.
"env": {
"NODE_ENV": "development"
},
// Use JavaScript source maps (if they exist).
"sourceMaps": true,
"request": "launch",
"outFiles": [
"${workspaceRoot}/dist/dev/*/*.js"
]
}
注意 type 的差異,它被設定為 node2,而 program 指向 ts 而不是 js。
希望這能幫上忙。
