有没有一种方法可以使用Visual Studio Code执行javascript并显示结果?
例如,一个脚本文件包含
console.log('hello world');
我认为需要使用nodejs,但不知道怎么做?
编辑:我所说的Visual Studio Code"是指微软的新代码编辑器,而不是用Visual Studio编写的代码。
**本方案打算在节点中运行当前打开的文件,并在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}"]
}
]
}
然后你就可以。 ``按F1键>;输入 "运行任务">;输入>;选择 "运行文件">;输入````。 来运行你的任务,但我发现为打开任务列表添加一个自定义键绑定更容易。
要添加键绑定,在VSCode用户界面菜单中,进入'Code'>'Preferences'>'Keyboard Shortcuts'。把这个添加到你的键盘快捷方式中。
{
"key": "cmd+r",
"command": "workbench.action.tasks.runTask"
}
当然,你可以选择任何你想要的组合键。
更新:
假设你运行JavaScript代码是为了测试,你可以通过将其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"
}
好吧,为了简单地运行代码并在控制台显示输出,你可以创建一个任务并执行它,与@canerbalci提到的差不多。
这样做的缺点是,你只能得到输出,仅此而已。
我真正想做的是能够对代码进行调试,比如说我想解决一个小的算法或尝试一个新的ES6功能,我运行它时发现有一些不对劲,我可以在VSC中进行调试。
所以,我没有为它创建一个任务,而是修改了这个目录下的.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的调试器中启动你当前所处的任何一个文件。它被设置为启动时停止。
要启动它,按F5键,在你想调试的文件中。
这很简单,当你在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
}
]
}