简体   繁体   English

让VS代码调试js代码时默认选择Node.js环境

[英]Make VS code choose Node.js environment by default when debugging js code

I am using VS code v1.45 to debug a simple nodejs file.我正在使用 VS code v1.45 来调试一个简单的 nodejs 文件。

I am testing out a simple node.js file.我正在测试一个简单的 node.js 文件。

var msg = 'Hello World';
console.log(msg);

When I press F5, I get prompted to choose the environment.当我按 F5 时,系统会提示我选择环境。 See screenshot below.请参阅下面的屏幕截图。

在此处输入图像描述

This is a hassle.这很麻烦。 How do I make VS code choose Node.js environment by default without prompting?如何让VS code默认选择Node.js环境而不提示?

My node.js is installed at C:\Program Files\nodejs\node.exe我的 node.js 安装在C:\Program Files\nodejs\node.exe

I will answer my own question.我会回答我自己的问题。

在此处输入图像描述

Click on "create a launch.json file" on the debug panel found on left-hand side.点击左侧调试面板上的“创建一个launch.json文件”。 Then, click Node.js when prompted.然后,在出现提示时单击 Node.js。 Subsequently, Node.js will be the default environment.随后,Node.js 将成为默认环境。

Credit goes to someone by the name of drunktimelord.归功于醉酒时间领主的名字。

I will build on answer by user3848207我将建立在 user3848207 的回答之上

Delete any existing launch.json file first.首先删除任何现有的 launch.json 文件。 Otherwise, no effect when you click on "create a launch.json file" on the debug panel.否则在调试面板点击“创建launch.json文件”无效。

Now, click on "create a launch.json file" on the debug panel found on left-hand side.现在,点击左侧调试面板上的“create a launch.json 文件”。 Then, click Node.js when prompted.然后,在出现提示时单击 Node.js。 Subsequently, Node.js will be the default environment.随后,Node.js 将成为默认环境。

Manually edit launch.json such that "program": "${file}" .手动编辑launch.json使得"program": "${file}" Otherwise, when you press F5, it will run on specific js file and not on actively open file.否则,当您按 F5 时,它将在特定的 js 文件上运行,而不是在主动打开的文件上运行。

launch.json should look like this. launch.json 应该是这样的。

在此处输入图像描述

This answer works on VS code v1.45这个答案适用于 VS 代码 v1.45

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

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