How can I bind a keyboard shortcut for npm script debug in VS Code?

38 views Asked by At

This code for configure test script to debug.

 {
  "name": "testproject",
  "version": "1.0.0",
  "description": "desc",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "mocha test.js",
    "test_unit": "mocha test_unit.js"
  },
}

This is a screenshot of VS Code with Bug Button which starts the test debug. It shows when I move mouse on it.

Bug Button

How I can set a hotkey on this button?

1

There are 1 answers

2
starball On

I don't think so? The relevant commands would be npm.debugScript, but the implementation of both those commands don't seem to be designed for use with keybindings. You can see the source code in https://github.com/microsoft/vscode/blob/078b98804ecf81cc7347a40604f63f4a494c9a71/extensions/npm/src/npmView.ts#L145. The furthest I can get in that direction is something like

{
    "key": "", // TODO
    "command": "npm.debugScript",
    "args": {
        "script": {
            "packageJson": { "path": ".../package.json" },
        },
        "task": { "definition": { "script": "build" } }, // TODO: replace "build" with the script you want to run
        "package": { "resourceUri": { "fsPath": ".../package.json" } },
    },
},

And at that point you'll run into "<minified variable name>.getFolder is not a function".

If you want keybinding support for this command, you'll probably have to raise a feature-request or raise a PR.


You can, however, at least bind running (not debugging) the task to a keyboard shortcut:

{
    "key": "", // TODO
    "command": "workbench.action.tasks.runTask",
    "args": {
        "task": "npm: build" // TODO: replace "build" with the name of the script you want to run
    },
},