在“Visual Studio代码”中运行Javascript

有没有办法执行javascript和显示结果使用Visual Studio代码?

例如一个脚本文件包含

console.log('hello world'); 

我假设nodejs将需要,但不能解决如何做到这一点?

编辑:“Visual Studio代码”我的意思是来自Microsoft的新的代码编辑器 – 不使用Visual Studio编写的代码

Visual Studio代码

该解决scheme打算在节点中运行当前打开的文件,并在VSCode中显示输出。

我有同样的问题,并发现新的介绍tasks有用的特定用例。 这是一个小麻烦,但这是我所做的:

在项目的根目录下创build一个.vscode目录,并在其中创build一个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菜单中select“代码”>“首选项”>“键盘快捷键”。 将其添加到您的键盘快捷键中:

 { "key": "cmd+r", "command": "workbench.action.tasks.runTask" } 

当然,你可以select任何你想要的组合键。

更新:

假设您正在运行JavaScript代码来testing它,您可以将其任务标记为testing任务,方法是将其isTestCommand属性设置为true ,然后可以将键绑定到workbench.action.tasks.test命令以进行单动作调用。

换句话说,你的tasks.json文件现在将包含:

 { "version": "0.1.0", "command": "node", "isShellCommand": true, "args": [ "--harmony" ], "tasks": [ { "taskName": "runFile", "isTestCommand": true, "suppressTaskName": true, "showOutput": "always", "problemMatcher": "$jshint", "args": ["${file}"] } ] } 

…和你的keybindings.json文件现在将包含:

 { "key": "cmd+r", "command": "workbench.action.tasks.test" } 

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

  1. 安装代码运行器扩展
  2. 在文本编辑器中打开JavaScript代码文件,然后使用快捷键Ctrl+Alt+N ,或按F1 ,然后select/键入Run Code ,代码将运行,输出将显示在输出窗口中。

此外,您可以select部分JavaScript代码并运行代码片段。 很方便!

集成terminal的快捷方式是(ctrl +`),然后inputnode <filename>

或者,您可以创build一个任务。 这是我的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" } 

从这里创build一个快捷方式。 这是我的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" } ] 

这将在Command Pallete中打开“运行”,但是您仍然必须使用鼠标键入或select要运行的任务,在此情况下为节点。 第二个快捷方式切换输出面板,已经有一个快捷方式,但这些键彼此相邻,更容易处理。

那么,只需运行代码并在控制台上显示输出,就可以创build一个任务并执行它,就像@canerbalci提到的那样。

这样做的缺点是你只会得到输出,就是这样。

我真正想做的是能够debugging代码,让我说试图解决一个小的algorithm或尝试一个新的ES6function,我运行它,有什么可疑的,我可以在VSC中debugging它。

所以,我没有为它创build一个任务,而是修改了这个目录中的.vscode / launch.json文件,如下所示:

 { "version": "0.2.0", "configurations": [ { "name": "Launch", "type": "node", "request": "launch", "program": "${file}", "stopOnEntry": true, "args": [], "cwd": "${fileDirname}", "runtimeExecutable": null, "runtimeArgs": [ "--nolazy" ], "env": { "NODE_ENV": "development" }, "externalConsole": false, "sourceMaps": false, "outDir": null } ] } 

它所做的就是在VSC的debugging器中启动你当前所在的文件。 它的设置停止开始。

要启动它,请在要debugging的文件中按F5键。

这很简单,当你在VS Code中创build一个新文件并运行它时,如果你已经没有configuration文件为你创build一个configuration文件,那么你需要设置的唯一东西就是“程序”值,并设置它到你的主JS文件的path,如下所示:

 { "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 } ] } 

我使用了Node Exec,不需要任何configuration,build立了你当前正在结束的文件,或者在VSCode中输出的文件。

https://marketplace.visualstudio.com/items?itemName=miramac.vscode-exec-node

有了一些configuration,你可以添加Babel做一些在飞行中。