是否有一种方法来执行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编写的代码。
当前回答
在VS代码中遵循这些步骤。[在Windows操作系统下执行]
创建新文件 在里面写javascript代码 保存文件为filename.js 转到调试菜单 点击“开始调试” 或者直接按F5
启动调试截图
js代码在终端输出的截图
编辑:阅读这篇关于JS for VSCode的最新配置和特性的文档:https://code.visualstudio.com/docs/languages/javascript
其他回答
在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",
},
]
}
这很简单,当你在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
}
]
}
用npm安装nodemon Init nodemon: npm Init 打开的包。Json,并将其更改为: { “名称”:“JavaScript”, “版本”:“1.0.0”, “描述”:“”, “主要”:“{文件名}. js”, "脚本":{ "test": "echo \"错误:没有测试指定的\" && exit 1", "start": "nodemon {filename}.js" }, “关键词”:[], “作者”:“”, “许可证”:“ISC” } 在终端上写这个命令:npm start
你必须设置Node.js环境变量在VS code中运行JavaScript代码。按照这些设置并创建路径。
——打开控制面板->系统->高级系统设置->环境变量 ——找到变量PATH,并将node.js文件夹路径添加为值。通常是C:\Program Files Nodejs;。如果变量不存在,则创建它。 —重新启动IDE或计算机。
如果您想知道节点可执行文件应该在C:\Program Files\nodejs文件夹中。
如果您需要检查您的路径,您可以通过右键单击文件资源管理器中的计算机或从控制面板中的安全设置来查看它。在那里选择高级系统设置。将打开一个对话框,选中Advanced选项卡。底部是一个按钮,环境变量。
只需安装nodemon并运行
nodemon your_file.js
在vs code终端上。