node.js 在 Visual Studio Code 中使用“preLaunchTasks”和命名任务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35327016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using "preLaunchTasks" and Naming a Task in Visual Studio Code
提问by Brent Arias
According to the documentation, it is possible to launch a program before debugging:
根据文档,可以在调试之前启动程序:
To launch a task before the start of each debug session, set the
preLaunchTaskto the nameof one of the tasks specified in tasks.json.
要在每个调试会话开始之前启动任务,请将 设置为tasks.json 中指定的任务之一
preLaunchTask的名称。
I've not seen example syntax of a "named" task, but the schema documentationreveals a property called taskName. I tried using that to link my launch.json preLaunchTasksto the task, but it didn't work. When I launched my program, Visual Studio Code reported this error:
我还没有看到“命名”任务的示例语法,但架构文档显示了一个名为taskName. 我尝试使用它来将我的 launch.json 链接preLaunchTasks到任务,但没有奏效。当我启动我的程序时,Visual Studio Code 报告了这个错误:
Could not find a unique task 'launch-core'. Make sure the task exists and that it has a unique name.
找不到独特的任务“启动核心”。确保任务存在并且它有一个唯一的名称。
My custom "named" task looked something like this:
我的自定义“命名”任务如下所示:
{
"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",
}
I then tried changing the property name from taskNameto just name, based on this link. That also didn't work.
然后我试图更改属性的名称taskName,只是name,基于此链接。那也没有用。
Intellisense gives no suggestions of how to name a task.
Intellisense 没有给出如何命名任务的建议。
Does anybody know how to uniquely name a task in the tasks.json file? What is the syntax? What is the property name?
有人知道如何在 tasks.json 文件中唯一命名任务吗?语法是什么?物业名称是什么?
Ultimately I'd like to execute two or three node.js processes before my own node.js app is launched. For example, I'd like to have the following three apps launched before my app is launched into the debugger:
最终,我想在启动我自己的 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'
If I'm working on a Windows box, my task might look something like this:
如果我在 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"
]
}
The above task using using the cmd startcapability. I'm not sure yet how to make several node tasks launch instead of one, but I can't even get one task to launch because of this task-naming issue.
上述任务使用cmdstart功能。我还不确定如何启动多个节点任务而不是一个,但由于此任务命名问题,我什至无法启动一项任务。
How do I name a task in the tasks.json file?
如何在 tasks.json 文件中命名任务?
采纳答案by zeroxx1986
So, if it's still relevant, or if someone finds this thread with the same problem, I've just figured it out how it works:
所以,如果它仍然相关,或者如果有人发现这个线程有同样的问题,我只是想知道它是如何工作的:
In the tasks.json, you need to create a 'tasks' array- code hint will help you with that - which holds an array of objects. Inside an object, you can have the 'taskName'key-value pair.
在tasks.json 中,您需要创建一个'tasks' 数组- 代码提示将帮助您 - 它包含一个对象数组。在对象内部,您可以拥有“taskName”键值对。
Example:
例子:
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"args": ["run-script", "webpack"],
"showOutput": "always",
"tasks": [
{
"taskName": "runwebpack",
"suppressTaskName": true
}
]
}
In my case, I had to run the npm run-script webpackcommand before running my project.
In the launch.jsonfile, the "preLaunchTask": "runwebpack"will work now.
就我而言,我必须npm run-script webpack在运行我的项目之前运行该命令。在launch.json文件中,"preLaunchTask": "runwebpack"现在可以工作了。
Note: the suppressTaskNameis true in my example. Omitting it, or setting it to false will result in VS Code appending the taskNameafter the command.
注意:suppressTaskName在我的例子中是正确的。省略它,或将其设置为 false 将导致 VS CodetaskName在命令之后追加。
A more general approach would be something like this:
更通用的方法是这样的:
{
"version": "0.1.0",
"command": "npm",
"isShellCommand": true,
"args": ["run-script"],
"showOutput": "always",
"tasks": [
{ "taskName": "webpack" }
]
}
With the latter example, you can extend the tasksarray with other scripts to be run also.
对于后一个示例,您可以tasks使用其他要运行的脚本来扩展数组。
Hint for my usage: npm run-script fetches what to do from the package.jsonfile's scriptsobject.
我的用法提示:npm run-script 从package.json文件的scripts对象中获取要执行的操作。
Edit: this works with VS Code 1.3.1
编辑:这适用于VS Code 1.3.1
回答by Logan
FWIW, I'm using VS Code 1.20.1and here's how I got my preLaunchTaskto work:
FWIW,我使用的是VS Code 1.20.1,这是我如何让我的preLaunchTask工作的:
In launch.json:
在launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
...
"preLaunchTask": "npm: build",
}
]
}
In my package.json:
在我的package.json:
{
...
"scripts": {
"build": "tsc"
...
}
}
回答by Eli Algranti
For version 2.0.0 configuration you now use labelinstead of taskName.
对于 2.0.0 版配置,您现在使用label而不是taskName.
package.json:
包.json:
...
"scripts": {
"tsc": "tsc",
...
}
...
launch.json (My source is in the srcdirectory and tsccompiles to the distdirectory):
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
}
]
}
tasks.json:
任务.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile",
"type": "npm",
"script": "tsc",
"problemMatcher": []
}
]
}
回答by slideshowp2
For vscode 1.36.1 (1.36.1):
对于 vscode 1.36.1 (1.36.1):
tasks.json:
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"]
}
]
}
launch.json:
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"
}
]
}
Before running node ${workspaceFolder}/dist/main.js, the preLaunchTaskwill run buildtask firstly which includes two subtasks: cleanand build. It will run cleantask firstly, then run buildtask.
在运行之前node ${workspaceFolder}/dist/main.js,preLaunchTask将build首先运行包含两个子任务的任务:clean和build。它会clean先运行任务,然后运行build任务。
You can specify the label of a task to preLaunchTaskoption.
您可以指定要preLaunchTask选项的任务标签。
回答by 1.21 gigawatts
The question title is:
问题标题是:
"Using “preLaunchTasks” and Naming a Task in Visual Studio Code
“在 Visual Studio Code 中使用“preLaunchTasks”和命名任务
I needed to define preLaunchTask***s***.
我需要定义 preLaunchTask***s***。
You can config multiple tasks using the dependsOn property described here
您可以使用这里描述的 dependsOn 属性配置多个任务
For example, a compound task in your tasks.json:
例如,tasks.json 中的复合任务:
{
"version": "2.0.0",
"tasks": [
{
"label": "Client Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceRoot}/client"
}
},
{
"label": "Server Build",
"command": "gulp",
"args": ["build"],
"options": {
"cwd": "${workspaceRoot}/server"
}
},
{
"label": "Build",
"dependsOn": ["Client Build", "Server Build"]
}
]
}
You can find more information about naming tasks here.
您可以在此处找到有关命名任务的更多信息。
回答by LimpingNinja
I've only really seen the taskName used in relation to Gulp; I'm sure there are others but nothing that I have much insight into. Perhaps this can get you off to a start with what you already have?
我只真正见过与 Gulp 相关的 taskName;我敢肯定还有其他人,但没有什么我有太多洞察力。也许这可以让你从你已经拥有的东西开始?

