是否有一种方法来执行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编写的代码。
当前回答
你必须设置Node.js环境变量在VS code中运行JavaScript代码。按照这些设置并创建路径。
——打开控制面板->系统->高级系统设置->环境变量 ——找到变量PATH,并将node.js文件夹路径添加为值。通常是C:\Program Files Nodejs;。如果变量不存在,则创建它。 —重新启动IDE或计算机。
如果您想知道节点可执行文件应该在C:\Program Files\nodejs文件夹中。
如果您需要检查您的路径,您可以通过右键单击文件资源管理器中的计算机或从控制面板中的安全设置来查看它。在那里选择高级系统设置。将打开一个对话框,选中Advanced选项卡。底部是一个按钮,环境变量。
其他回答
这个解决方案打算运行当前打开的文件在节点和显示输出在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"
}
在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",
},
]
}
过程
*(前言)
步骤如下: 根据我的记忆,没有严格测试步骤…(可能会错过一些东西?) 以后的版本可能会有一些不同的变化 事物的自动设置(例如:环境路径)可能是不同的 在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 Code
你需要做的是在用户设置中设置node.js路径
您需要设置路径,因为您安装它在您的Windows机器。
我的是“C:\\Program Files\\nodejs\\node.exe\”
因为我的文件目录名中有一个空格
请看下面的图片。我未能在第一次运行代码,因为我在路径名称中犯了一个错误
希望这对你有所帮助。
当然,你的问题帮助了我,因为我也来这里寻求帮助,在我的VS CODE中运行JS
我建议你使用一个简单易用的插件,叫做Quokka,它现在非常流行,可以帮助你在运行中调试代码。 Quokka.js。使用这个插件最大的好处是你可以节省很多时间去浏览网页浏览器和评估你的代码,有了这个帮助,你可以看到在VS代码中发生的一切,这节省了很多时间。