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

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

console.log('hello world');

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

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


当前回答

另一种方法是打开终端ctrl+ '执行节点。现在您有了一个活动的节点REPL。您现在可以将您的文件或选定的文本发送到终端。为了做到这一点,打开VSCode命令面板(F1或ctrl+shift+p)并执行>在活动终端运行选定的文本或>在活动终端运行活动文件。

如果在执行代码之前需要一个干净的REPL,则必须重新启动节点REPL。这是在终端中使用节点REPL ctrl+c ctrl+c退出它,并键入node开始新的。

您可能可以将命令面板命令按键绑定到您希望的任何键。

node应该安装在您的路径中

其他回答

这很简单,当你在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
        }
    ]
}

我很惊讶居然没有人提到这一点:

只需在VS Code中打开有问题的.js文件,切换到“调试控制台”选项卡,点击左侧导航栏中的调试按钮,然后点击运行图标(播放按钮)!

需要安装nodejs !

你不需要在visual studio代码中设置运行javascript、python等代码的环境,你所要做的就是安装代码运行器扩展,然后选择你想要运行的代码的一部分,然后点击右上角的运行按钮。

在VS代码中遵循这些步骤。[在Windows操作系统下执行]

创建新文件 在里面写javascript代码 保存文件为filename.js 转到调试菜单 点击“开始调试” 或者直接按F5

启动调试截图

js代码在终端输出的截图

编辑:阅读这篇关于JS for VSCode的最新配置和特性的文档:https://code.visualstudio.com/docs/languages/javascript

对于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".