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

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

console.log('hello world');

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

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


当前回答

集成终端的快捷方式是ctrl + ',然后键入node <filename>。

或者,您可以创建一个任务。这是唯一的代码在我的tasks.json:

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "0.1.0",
"command": "node",
"isShellCommand": true,
"args": ["${file}"],
"showOutput": "always"
}

从这里创建一个捷径。这是我的keybindings.json:

// Place your key bindings in this file to overwrite the defaults
[
{   "key": "cmd+r",
"command": "workbench.action.tasks.runTask"
},
{   "key": "cmd+e",
"command": "workbench.action.output.toggleOutput"
}
]

这将在命令面板中打开“运行”,但你仍然需要用鼠标键入或选择你想要运行的任务,在这种情况下是节点。第二个快捷键切换输出面板,它已经有一个快捷键,但这些键彼此相邻,更容易使用。

其他回答

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

有一个更简单的方法来运行JavaScript,不需要配置:

安装代码运行器扩展 在文本编辑器中打开JavaScript代码文件,然后使用快捷键Control+Alt+N(或者在macOS上使用⌃Control+“选项”+“N”),或者按F1,然后选择/键入“运行代码”,代码将运行,输出将显示在“输出窗口”中。

此外,您还可以选择部分JavaScript代码并运行代码片段。扩展也适用于未保存的文件,所以你可以创建一个文件,将其更改为Javascript并快速编写代码(当你只需要尝试一些快速的东西)。非常方便!

这需要NodeJS,否则它将无法工作。

我面对这个确切的问题,当我第一次开始使用扩展代码运行器VS Code

你需要做的是在用户设置中设置node.js路径

您需要设置路径,因为您安装它在您的Windows机器。

我的是“C:\\Program Files\\nodejs\\node.exe\”

因为我的文件目录名中有一个空格

请看下面的图片。我未能在第一次运行代码,因为我在路径名称中犯了一个错误

希望这对你有所帮助。

当然,你的问题帮助了我,因为我也来这里寻求帮助,在我的VS CODE中运行JS

你必须设置Node.js环境变量在VS code中运行JavaScript代码。按照这些设置并创建路径。

——打开控制面板->系统->高级系统设置->环境变量 ——找到变量PATH,并将node.js文件夹路径添加为值。通常是C:\Program Files Nodejs;。如果变量不存在,则创建它。 —重新启动IDE或计算机。

如果您想知道节点可执行文件应该在C:\Program Files\nodejs文件夹中。

如果您需要检查您的路径,您可以通过右键单击文件资源管理器中的计算机或从控制面板中的安全设置来查看它。在那里选择高级系统设置。将打开一个对话框,选中Advanced选项卡。底部是一个按钮,环境变量。

如果您的节点安装在您的机器上

只要在VSCODE中打开终端,输入node yourfile.js,就是这样