【问题标题】:VS Code: Using Grunt from node_modules\.binVS 代码:使用来自 node_modules\.bin 的 Grunt
【发布时间】:2016-03-25 18:26:24
【问题描述】:

我正在尝试使用 Visual Studio Code、Node.js 和 Grunt,但遇到了问题。本质上,我似乎无法说服 VS Code 从 npm 放置的本地 node_modules\.bin 位置运行 grunt。如果我在全球范围内安装gruntgrunt-cli,一切正常,但就个人而言,我不明白为什么我必须这样做。

我有一个如下所示的package.json 文件:

{
    "version": "1.0.0",
    "name": "VSCODETEST",
    "private": true,
    "devDependencies": {

        "grunt": "~0.4.5",
        "grunt-cli": "~1.1.0",

        "typescript": "~1.8.9",
        "grunt-typescript": "~0.8.0"

    },

    "dependencies": {
        "jquery": "~2.2.2",
        "bootstrap": "4.0.0-alpha.2"
    }
}

运行npm install 下载依赖层次结构。

我在我的gruntfile.js 中定义了各种任务,在我的项目的根目录中,这个任务文件在.vscode\tasks.json 中:

{
    "version": "0.1.0",
    "command": "grunt",
    "isShellCommand": true,
    "tasks": []
}

我知道这行得通,因为在全局安装工具后...

npm install -g grunt grunt-cli

... VS Code 会自动选择 gruntfile 中的任务并在 UI 中显示给我。

但是,如果我不全局安装它们(或者我卸载它们,全局),它会给我这个错误:

'grunt' is not recognized as an internal or external command,
operable program or batch file.

显然,它在其路径中找不到grunt,所以我尝试通过修改任务文件来解决这个问题...

{
    "version": "0.1.0",
    "command": "${workspaceRoot}\\node_modules\\.bin\\grunt",
    "isShellCommand": true,
    "tasks": []
}

这解决了错误,但我不再列出任何任务 - 我只得到“未找到任务”

如何在 VS Code 用来查找其工具的 PATH 中包含 node_modules\.bin 或以其他方式解决此问题?

【问题讨论】:

    标签: node.js gruntjs npm visual-studio-code


    【解决方案1】:

    我找到了解决方案:.vscode\tasks.json 应该是这样的:

    {
        "version": "0.1.0",
        "command": "${cwd}/node_modules/.bin/grunt",
        "isShellCommand": true,
        "showOutput": "always",
        "tasks": [
            {
                "taskName": "default",
                "args": [],
                "isBuildCommand": true,
                "problemMatcher": "$tsc-watch"
            }
        ]
    }
    

    重要的变化是要运行的command 的规范。

    另外,在gruntfile.js 中,需要实际注册与在VS Code 的配置文件中分配给任务的taskName 匹配的任务。例如,上面我的示例中的default 任务注册如下:

    // Default Task is to run grunt-ts build task named `ts'
    grunt.registerTask("default", ["ts"]);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 2012-02-05
      • 1970-01-01
      • 2019-05-11
      • 1970-01-01
      相关资源
      最近更新 更多