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

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

console.log('hello world');

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

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


当前回答

使用这个类名tempCodeRunnerFile运行java文件而不保存

public class tempCodeRunnerFile{
    public static void main(String[] args) {
        System.out.println("aaaaaa");
    }
}

其他回答

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

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

使用这个类名tempCodeRunnerFile运行java文件而不保存

public class tempCodeRunnerFile{
    public static void main(String[] args) {
        System.out.println("aaaaaa");
    }
}

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

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

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

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

过程

*(前言)

步骤如下: 根据我的记忆,没有严格测试步骤…(可能会错过一些东西?) 以后的版本可能会有一些不同的变化 事物的自动设置(例如:环境路径)可能是不同的 在Win10

运行纯js

download & install Vscode download & install Node.js from the the offical website, eg: in G:\nodejs\node.exe open Vscode -> open your workspace folder eg: MyTestSpace -> create a new file call eg: test.js write a code console.log('-----test-----'); in test.js go to the Run and Debug panel (ctrl+shift+d) > Run drop down list at the top > Add Config (MyTestSpace) > a launch.json should be auto generated for you > at the auto-completetion popup > select Node.js: Launch Program > auto complete config fill in > rename the program to the path where your test.js locate your launch.json should look something like this:: { "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Launch Program", "program": "${workspaceFolder}/test.js", }, ], } go back to the Run drop down list > select Launch Program (MyTestSpace) > click the Run button in your Debug Console should see the following:: G:\nodejs\node.exe .\test.js -----test----- a graph for demo naming might be different (just a ugly rough draft for demo [[(just try to post an answer wrote with little effort...)]]) (ignore unnecessary staff...)

运行js & html(简要介绍)

have a eg: main_test.html file, with a <script> tag refer to the test.js in the Run drop down list > add config > select Chrome > auto complete launch.json > change path to main_test.html eg: { "type": "chrome", "request": "launch", "name": "Open main_test.html", "file": "h:\\Book\\debug\\LXPI\\OEBPS\\lib_new2\\libNt\\crossFileHtse\\build_HighlightTypeset_ReadHtse\\main_test.html", }, select that chrome launch config > Run > html is opened up in your browser & console log printed

在Visual Studio Code中运行javascript有很多方法。

如果您使用Node,那么我建议使用VSC中的标准调试器。

我通常创建一个虚拟文件,比如test.js,在那里我做外部测试。

在存放代码的文件夹中,创建名为“”的文件夹。然后创建一个名为launch。json的文件

在此文件中粘贴以下内容并保存。现在您有两个选项来测试您的代码。

当你选择“Nodemon Test File”时,你需要把你的代码放在Test .js中进行测试。

要安装nodemon以及更多关于如何在VSC中调试nodemon的信息,我建议阅读这篇文章,它更详细地解释了发布的第二部分。json文件和如何调试在ExpressJS。

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Nodemon Test File",
            "runtimeExecutable": "nodemon",
            "program": "${workspaceFolder}/test.js",
            "restart": true,
            "console": "integratedTerminal",
            "internalConsoleOptions": "neverOpen"
        },
        {
            "type": "node",
            "request": "attach",
            "name": "Node: Nodemon",
            "processId": "${command:PickProcess}",
            "restart": true,
            "protocol": "inspector",
        },
    ]
}