【问题标题】:VS Code and tasks with nodeVS Code 和带节点的任务
【发布时间】:2015-11-01 16:02:32
【问题描述】:

到目前为止,我使用 gulp 来构建 typescript 和 sass 文件,但现在由于几个新的构建步骤,我想统一所有内容并将节点用作单个入口点(也是通过 npm run 运行 gulp 任务的节点任务名称)。

tasks.json 很简单,任务build 应该运行npm run watch

{
    "version": "0.1.0",
    "command": "npm",
    "isShellCommand": true,
    "tasks": [
        {
            "taskName": "build",
            "isBuildCommand": true,
            "showOutput": "always",
            "isWatching": true,
            "args": [
                "run", "watch"
            ]
        }
    ]
}

package.json

"scripts": {
    "watch": "gulp default",
}

还有输出:

gulp default build
[14:20:54] Using gulpfile PATH_TO/gulpfile.js
[14:20:54] Task 'build' is not in your gulpfile
[14:20:54] Please check the documentation for proper gulpfile formatting
npm ERR! Windows_NT 6.3.9600
npm ERR! argv "C:\\Program Files (x86)\\nodejs\\\\node.exe" "C:\\Program Files (x86)\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "watch" "build"
npm ERR! node v0.12.2
npm ERR! npm  v2.7.4
npm ERR! code ELIFECYCLE
npm ERR! 2@0.1.0 watch: `gulp default build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the 2@0.1.0 watch script 'gulp default build'.
npm ERR! This is most likely a problem with the 2 package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     gulp default build
npm ERR! You can get their info via:
npm ERR!     npm owner ls 2
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:

根据输出,即使在 tasks.json 中没有任何迹象(gulpfile.json 存在于根目录中,并且在搜索解决方案时我发现 VS Code 自动检测到它,我仍然以某种方式使用 gulp假设可能是问题所在?)。此外,taskName 属性看起来像自动附加到命令行作为错误的参数。

一个较小但有效的示例(但它仍然运行 gulp,因此每次保存时都会编译两次 typescript):

{
    "version": "0.1.0",
    "command": "npm",
    "isShellCommand": true,
    "args": [
        "run", "watch"
    ],
    "showOutput": "always"
}

如何通过 npm 在 VS Code 中执行多项任务?

【问题讨论】:

  • 我也在寻找类似的解决方案。关于附加到命令行的 taskName,请查找 suppressTaskName 配置属性,如提到的 here
  • 另外,看看this 了解如何从 VS Code 运行 npm 脚本任务

标签: npm visual-studio-code


【解决方案1】:

如我的 cmets 中所述,如果您希望从 VS Code 运行 npm 脚本任务,请查找基本上指示创建 .vscode\tasks.jsonthis article,如下所示:

{
    "version": "0.1.0",
    "command": "npm",
    "isShellCommand": true,
    "suppressTaskName": true,
    "tasks": [
        {
        // Build task, Ctrl+Shift+B
        // "npm install --loglevel info"
        "taskName": "install",
        "isBuildCommand": true,
        "args": ["install", "--loglevel", "info"]
        },
        {
        // Test task, Ctrl+Shift+T
        // "npm test"
        "taskName": "test",
        "isTestCommand": true,
        "args": ["test"]
        },
        {
        // "npm run lint"
        "taskName": "lint",
        "args": ["run", "lint"]
        }
    ]
}

作为替代方案,还有一个来自 Microsoft 的示例 VS Code 扩展,专门用于检测和运行 npm 脚本项:vscode-npm-scripts

【讨论】:

  • 但是如果主命令是 npm,你如何运行 tsc 编译器?不是每个任务都有自己的命令吗?
  • 对不起,我不明白。如果您有一个基于 grunt-/gulp- 的工具链,则 grunt/gulp 有责任在其任务/步骤之一中在后台运行 tsc 编译器。如果是这种情况,我猜你只是从 npm 脚本调用特定的 grunt/gulp 任务。如果我弄错了,请进一步澄清
  • 感谢您的回答。如果我没记错的话,在我写这个问题时,上面的解决方案不起作用。但现在是正确的答案。
  • 顺便感谢@Izhaki 将 JSON 更新到最新的 VS Code
猜你喜欢
  • 2020-12-06
  • 2018-08-20
  • 2022-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-04
  • 1970-01-01
相关资源
最近更新 更多