简体   繁体   English

如何使用 VSCode 自动运行 Node.js 程序?

[英]How to automatically run a Node.js program using VSCode?

I was wondering whether there is a way to execute a Node.js program using some keyboard shortcut in Visual Studio Code.我想知道是否有办法在 Visual Studio Code 中使用一些键盘快捷键来执行 Node.js 程序。

For instance, while I am coding in the editor, instead of having to manually open the Integrated Terminal in Visual Studio Code and then type let's say node app.js , can I just configure a key to automatically do this?例如,当我在编辑器中编码时,不必在 Visual Studio Code 中手动打开集成终端,然后输入node app.js ,我可以只配置一个键来自动执行此操作吗?

Below shown is an illustration of what I want:下面显示的是我想要的插图:

在此处输入图片说明

I want to run the file app.js in the terminal without having to go there again and again and type node app (or even by using the up arrow key to bring the last entry from the terminal's history).我想在终端中运行文件app.js ,而不必一次又一次地去那里并输入node app (甚至使用向上箭头键从终端的历史记录中获取最后一个条目)。

Note: I found one way of doing so and that is to go to the Debug console .注意:我找到了一种方法,那就是转到调试控制台 But sometimes, it doesn't display full results.但有时,它不会显示完整的结果。 For instance, when logging a module, it displays one line with a caret which can be used to inspect details of the logged object further, but when I press the caret, it says that there is no debugger to view the object .例如,当记录一个模块时,它会显示一行带有插入符号的行,可用于进一步检查记录对象的详细信息,但是当我按下插入符号时,它说没有调试器可以查看对象

You can create a shell task or an npm task that runs node app.js .您可以创建一个 shell任务或一个运行node app.js的 npm任务 You can then bind a hotkey for running a task in the VS Code key binding settings.然后,您可以在 VS Code 键绑定设置中绑定用于运行任务的热键。 The npm extension should automatically detect every script in the package.json file and provide them as a task if the task.autoDetect setting is set to on.如果task.autoDetect设置为 on,npm 扩展应自动检测 package.json 文件中的每个脚本并将它们作为任务提供。 You can simply add a start script that runs node app.js .您可以简单地添加一个运行node app.js的启动脚本。 I have then set up a hotkey that reruns the latest task: { "key": "ctrl+rb", "command": "workbench.action.tasks.reRunTask" },然后我设置了一个重新运行最新任务的热键: { "key": "ctrl+rb", "command": "workbench.action.tasks.reRunTask" },

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM