Bre*_*ias 28 node.js visual-studio-code
根据文档,可以在调试之前启动程序:
要在每个调试会话开始之前启动任务,请将其设置为tasks.json中指定的任务之一
preLaunchTask
的名称.
我没有看到"命名"任务的示例语法,但架构文档显示了一个名为的属性taskName
.我尝试使用它将我的launch.json链接preLaunchTasks
到任务,但它没有用.当我启动我的程序时,Visual Studio Code报告了此错误:
无法找到一个独特的任务'launch-core'.确保该任务存在且它具有唯一的名称.
我的自定义"命名"任务看起来像这样:
{
"taskName": "launch-core",
"version": "0.1.0",
"command": "C:\\utils\\mystuff.exe",
// The command is a shell script
"isShellCommand": true,
// Show the output window only if unrecognized errors occur.
"showOutput": "silent",
}
Run Code Online (Sandbox Code Playgroud)
然后我试图更改属性的名称taskName
,只是name
,基于此链接.这也行不通.
Intellisense没有提供如何命名任务的建议.
有人知道如何在tasks.json文件中唯一地命名任务吗?语法是什么?什么是物业名称?
最终我想在我自己的node.js应用程序启动之前执行两个或三个node.js进程.例如,我想在我的应用程序启动到调试器之前启动以下三个应用程序:
sh -c 'cd ./manager/ && node manager.js'
sh -c 'cd ./adapter/ && node adapter.js'
sh -c 'cd ./core/ && node core.js'
Run Code Online (Sandbox Code Playgroud)
如果我正在使用Windows机器,我的任务可能如下所示:
{
"taskName": "core-launch",
"version": "0.1.0",
// The command is tsc. Assumes that tsc has been installed using npm install -g typescript
"command": "start",
// The command is a shell script
"isShellCommand": true,
// Show the output window only if unrecognized errors occur.
"showOutput": "silent",
// args is the HelloWorld program to compile.
"args": [
"ACD-Manager",
"/B",
"/D",
"./manager/",
"node",
"manager.js"
]
}
Run Code Online (Sandbox Code Playgroud)
以上任务使用cmd start
功能.我不确定如何启动多个节点任务而不是一个,但由于此任务命名问题,我甚至无法启动一个任务.
如何在tasks.json文件中命名任务?
小智 20
所以,如果它仍然相关,或者如果有人发现这个线程有同样的问题,我只是弄清楚它是如何工作的:
在tasks.json中,你需要创建一个'tasks'数组 - 代码提示将帮助你 - 它拥有一个对象数组.在对象内部,您可以拥有'taskName'键值对.
例:
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"args": ["run-script", "webpack"],
"showOutput": "always",
"tasks": [
{
"taskName": "runwebpack",
"suppressTaskName": true
}
]
}
Run Code Online (Sandbox Code Playgroud)
就我而言,我必须npm run-script webpack
在运行项目之前运行命令.在launch.json文件中,"preLaunchTask": "runwebpack"
现在可以正常工作.
注意:suppressTaskName
在我的例子中是正确的.省略它或将其设置为false将导致VS代码taskName
在命令后附加.
更通用的方法是这样的:
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"args": ["run-script"],
"showOutput": "always",
"tasks": [
{ "taskName": "webpack" }
]
}
Run Code Online (Sandbox Code Playgroud)
在后一个示例中,您可以tasks
使用其他脚本扩展数组以进行运行.
提示我的用法:npm run-script从package.json文件的scripts
对象中获取要做的事情.
编辑:这适用于VS Code 1.3.1
Log*_*gan 18
FWIW,我正在使用VS Code 1.20.1,这是我如何让我的preLaunchTask工作:
在launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
...
"preLaunchTask": "npm: build",
}
]
}
Run Code Online (Sandbox Code Playgroud)
在我的package.json
:
{
...
"scripts": {
"build": "tsc"
...
}
}
Run Code Online (Sandbox Code Playgroud)
Eli*_*nti 11
对于2.0.0版配置,您现在使用label
而不是taskName
。
package.json:
...
"scripts": {
"tsc": "tsc",
...
}
...
Run Code Online (Sandbox Code Playgroud)
launch.json(我的源代码在src
目录中,并tsc
编译到该dist
目录中):
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"preLaunchTask": "Compile",
"name": "Launch Program",
"program": "${workspaceFolder}/src/index.ts",
"outFiles": [
"${workspaceFolder}/dist/**/*.js"
],
"protocol": "inspector",
"sourceMaps": true
}
]
}
Run Code Online (Sandbox Code Playgroud)
task.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile",
"type": "npm",
"script": "tsc",
"problemMatcher": []
}
]
}
Run Code Online (Sandbox Code Playgroud)
对于 vscode 1.36.1 (1.36.1)
:
tasks.json
:
{
"version": "2.0.0",
"tasks": [
{
"label": "build:tsc",
"type": "npm",
"script": "build:tsc"
},
{
"label": "clean",
"type": "npm",
"script": "clean"
},
{
"label": "build",
"dependsOrder": "sequence",
"dependsOn": ["clean", "build:tsc"]
}
]
}
Run Code Online (Sandbox Code Playgroud)
launch.json
:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/dist/main.js",
"preLaunchTask": "build",
"outFiles": ["${workspaceFolder}/dist/**/*.js"],
"console": "integratedTerminal"
}
]
}
Run Code Online (Sandbox Code Playgroud)
在运行之前node ${workspaceFolder}/dist/main.js
,preLaunchTask
将build
首先运行包含两个子任务的任务:clean
和build
。它会clean
先运行任务,然后运行build
任务。
您可以指定要preLaunchTask
选项的任务标签。
归档时间: |
|
查看次数: |
25110 次 |
最近记录: |