是否有一种方法来执行JavaScript和显示使用Visual Studio代码的结果?
例如,一个脚本文件包含:
console.log('hello world');
我假设Node.js是需要的,但不知道如何做到这一点?
通过Visual Studio Code,我指的是来自微软的新代码编辑器 不是用Visual Studio编写的代码。
是否有一种方法来执行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应该安装在您的路径中
其他回答
过程
*(前言)
步骤如下: 根据我的记忆,没有严格测试步骤…(可能会错过一些东西?) 以后的版本可能会有一些不同的变化 事物的自动设置(例如:环境路径)可能是不同的 在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
在VS代码中遵循这些步骤。[在Windows操作系统下执行]
创建新文件 在里面写javascript代码 保存文件为filename.js 转到调试菜单 点击“开始调试” 或者直接按F5
启动调试截图
js代码在终端输出的截图
编辑:阅读这篇关于JS for VSCode的最新配置和特性的文档:https://code.visualstudio.com/docs/languages/javascript
用npm安装nodemon Init nodemon: npm Init 打开的包。Json,并将其更改为: { “名称”:“JavaScript”, “版本”:“1.0.0”, “描述”:“”, “主要”:“{文件名}. js”, "脚本":{ "test": "echo \"错误:没有测试指定的\" && exit 1", "start": "nodemon {filename}.js" }, “关键词”:[], “作者”:“”, “许可证”:“ISC” } 在终端上写这个命令:npm start
另一种方法是打开终端ctrl+ '执行节点。现在您有了一个活动的节点REPL。您现在可以将您的文件或选定的文本发送到终端。为了做到这一点,打开VSCode命令面板(F1或ctrl+shift+p)并执行>在活动终端运行选定的文本或>在活动终端运行活动文件。
如果在执行代码之前需要一个干净的REPL,则必须重新启动节点REPL。这是在终端中使用节点REPL ctrl+c ctrl+c退出它,并键入node开始新的。
您可能可以将命令面板命令按键绑定到您希望的任何键。
node应该安装在您的路径中
从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