是否有一种方法来执行JavaScript和显示使用Visual Studio代码的结果?

例如,一个脚本文件包含:

console.log('hello world');

我假设Node.js是需要的,但不知道如何做到这一点?

通过Visual Studio Code,我指的是来自微软的新代码编辑器 不是用Visual Studio编写的代码。


当前回答

从v1.32开始,这可能是最简单的:

{
    "key": "ctrl+shift+t",
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "node '${file}'\u000D" }
  }

使用您自己的键绑定。

参见发布说明:sendSequence和变量。

使用vscode v1.32,您可以使用像${file}这样的变量将sequence发送到终端,这是当前文件。如果你想要一些其他的路径,在上面的键绑定中用你的路径名替换${file}。

\u000D是一个返回,因此它将立即运行。

我在${file}变量周围添加了's,以防你的文件路径中有空格, 比如c:Users\Some Directory\fileToRun

其他回答

我建议你使用一个简单易用的插件,叫做Quokka,它现在非常流行,可以帮助你在运行中调试代码。 Quokka.js。使用这个插件最大的好处是你可以节省很多时间去浏览网页浏览器和评估你的代码,有了这个帮助,你可以看到在VS代码中发生的一切,这节省了很多时间。

用npm安装nodemon Init nodemon: npm Init 打开的包。Json,并将其更改为: { “名称”:“JavaScript”, “版本”:“1.0.0”, “描述”:“”, “主要”:“{文件名}. js”, "脚本":{ "test": "echo \"错误:没有测试指定的\" && exit 1", "start": "nodemon {filename}.js" }, “关键词”:[], “作者”:“”, “许可证”:“ISC” } 在终端上写这个命令:npm start

这个解决方案打算运行当前打开的文件在节点和显示输出在VSCode。

我也有同样的问题,并且发现新引入的任务对于这个特定的用例很有用。这有点麻烦,但我是这么做的:

在项目的根目录中创建一个.vscode目录,并创建一个tasks。Json文件。将这个任务定义添加到文件中:

{
    "version": "0.1.0",
    "command": "node",
    "isShellCommand": true,
    "args": [
        "--harmony"
    ],

    "tasks": [
        {
            "taskName": "runFile",
            "suppressTaskName": true,
            "showOutput": "always",
            "problemMatcher": "$jshint",
            "args": ["${file}"]
        }
    ]
}

然后你可以: press F1 > type ' run task ' > enter > select ' runFile ' > enter 来运行任务,但我发现为打开任务列表添加自定义键绑定更容易。

要添加键绑定,在VSCode UI菜单中,点击“Code”>“Preferences”>“Keyboard Shortcuts”。添加到你的键盘快捷键:

{
    "key": "cmd+r",
    "command": "workbench.action.tasks.runTask"
}

当然,你可以选择任何你想要的组合键。

更新:

假设您正在运行JavaScript代码来测试它,您可以通过将其isTestCommand属性设置为true来将您的任务标记为测试任务,然后您可以将一个键绑定到workbench.action.tasks.test命令以进行单动作调用。

换句话说,就是你的任务。Json文件现在将包含:

{
    "version": "0.1.0",
    "command": "node",
    "isShellCommand": true,
    "args": [
        "--harmony"
    ],

    "tasks": [
        {
            "taskName": "runFile",
            "isTestCommand": true,
            "suppressTaskName": true,
            "showOutput": "always",
            "problemMatcher": "$jshint",
            "args": ["${file}"]
        }
    ]
}

...还有你的按键。Json文件现在将包含:

{
    "key": "cmd+r",
    "command": "workbench.action.tasks.test"
}

这很简单,当你在VS Code中创建一个新文件并运行它时,如果你还没有配置文件,它会为你创建一个,你唯一需要设置的是“program”值,并将其设置为你的主JS文件的路径,如下所示:

{
    "version": "0.1.0",
    // List of configurations. Add new configurations or edit existing ones.  
    // ONLY "node" and "mono" are supported, change "type" to switch.
    // ABSOLUTE paths are required for no folder workspaces.
    "configurations": [
        {
            // Name of configuration; appears in the launch configuration drop down menu.
            "name": "Launch",
            // Type of configuration. Possible values: "node", "mono".
            "type": "node",
            // ABSOLUTE path to the program.
            "program": "C:\\test.js", //HERE YOU PLACE THE MAIN JS FILE
            // Automatically stop program after launch.
            "stopOnEntry": false,
            // Command line arguments passed to the program.
            "args": [],
            // ABSOLUTE path to the working directory of the program being debugged. Default is the directory of the program.
            "cwd": "",
            // ABSOLUTE path to the runtime executable to be used. Default is the runtime executable on the PATH.
            "runtimeExecutable": null,
            // Optional arguments passed to the runtime executable.
            "runtimeArgs": [],
            // Environment variables passed to the program.
            "env": { },
            // Use JavaScript source maps (if they exist).
            "sourceMaps": false,
            // If JavaScript source maps are enabled, the generated code is expected in this directory.
            "outDir": null
        }, 
        {
            "name": "Attach",
            "type": "node",
            // TCP/IP address. Default is "localhost".
            "address": "localhost",
            // Port to attach to.
            "port": 5858,
            "sourceMaps": false
        }
    ]
}

对于windows:只需将.js文件的文件关联更改为node.exe

1) Take VSCode
2) Right click on the file in left pane
3) Click "Reveal in explorer" from context menu
4) Right click on the file -> Select "Open with" -> Select "Choose another program"
5) Check box "Always use this app to open .js file"
6) Click "More apps" -> "Look for another app in PC"
7) Navigate to node.js installation directory.(Default C:\Program Files\nodejs\node.exe"
8) Click "Open" and you can just see cmd flashing
9) Restart vscode and open the file -> Terminal Menu -> "Run active file".