Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code and tasks with node

Until now I used gulp for building typescript and sass files, but now due to a couple of new build steps I'd like to unify everything and use node as a single entry point (also node for running gulp tasks via npm run taskName).

tasks.json is quite simple, task build should run 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",
}

And the output:

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! [email protected] watch: `gulp default build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] 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:

Based on the output, gulp is still somehow used even thought there is no sign of it in tasks.json (gulpfile.json exists in root directory and while searching for a solution I found that VS Code auto detects it, which I assume might be the problem?). Also taskName property is looks like automatically appended to command line as an argument which is wrong.

A smaller but working example (but it still runs gulp therefore typescript is compiled twice on each save):

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

How can I have multiple tasks with in VS Code through npm?

like image 983
Jaka Konda Avatar asked Nov 01 '15 16:11

Jaka Konda


People also ask

Does VS Code use node?

Visual Studio Code has support for the JavaScript and TypeScript languages out-of-the-box as well as Node. js debugging. However, to run a Node. js application, you will need to install the Node.

How do I run a task in VS Code?

Tip: You can run your task through Quick Open (Ctrl+P) by typing 'task', Space and the command name.


1 Answers

As mentioned in my comments, if you're looking to run npm scripts tasks from VS Code, look for this article that basically instructs to create a .vscode\tasks.json like the following:

{
    "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"]
        }
    ]
}

As an alternative, there's also a sample VS Code extension from Microsoft specifically aimed at detecting and running npm scripts items: vscode-npm-scripts

like image 152
superjos Avatar answered Oct 18 '22 05:10

superjos