VS代码和节点任务

时间:2015-11-01 16:02:33

标签: npm visual-studio-code

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

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:

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

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

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

如何通过npm在VS Code中拥有多个任务?

1 个答案:

答案 0 :(得分:19)

正如我在评论中所提到的,如果您正在寻找从VS Code运行npm脚本任务,请查找this article,它基本上指示创建.vscode\tasks.json,如下所示:

{
    "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代码扩展示例,专门用于检测和运行npm脚本项:vscode-npm-scripts