简体   繁体   English

Visual Studio Code 中调试控制台中的 Node.js readline

[英]Node.js readline in debug-console in Visual Studio Code

I'm new to Visual Studio Code, Javascript and Node.js.我是 Visual Studio Code、Javascript 和 Node.js 的新手。

Coming from C# I want to debug something like this with breakpoints set in Debugging-console:来自 C# 我想用在 Debugging-console 中设置的断点来调试这样的东西:

var name = Console.readline();
Console.Writeline(name);

It seems to be so simple, but I fail.看起来很简单,但我失败了。 What I found so far is, that after到目前为止我发现的是,之后

npm install sget 

I can run app.js in integrated terminal and it will work interactively.我可以在集成终端中运行 app.js,它将以交互方式工作。 But that ignores my breakpoints.但这忽略了我的断点。

So, how can I work with readline-functionality and breakpoints set?那么,我如何使用 readline 功能和断点集?

Thx -piccus Thx -piccus

EDIT: Thank you Siam,编辑:谢谢暹罗,

I already found the Code you referenced.我已经找到了你引用的代码。 After putting放置后

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

rl.question('What do you think of Node.js? ', (answer) => {
  // TODO: Log the answer in a database
  console.log(`Thank you for your valuable feedback: ${answer}`);

  rl.close();
});

into my app.js and pressing F5, the Debug-Control-Panel prompts:进入我的 app.js 并按 F5, Debug-Control-Panel提示:

node --debug-brk=38079 --nolazy app.js 
Debugger listening on [::]:38079
What do you think of Node.js? 

Unfortunately that is the end of Debugging.不幸的是,调试结束了。 Whatever I fill into the commandline below that Debug-Control-Panel, the result is always无论我在调试控制面板下面的命令行中填写什么,结果总是

nicht verfügbar  

what probably stands for 'not available'.什么可能代表“不可用”。 No further breakpoint is being reached.没有到达进一步的断点。

Running that Code from seperate powershell will run as it should, but of Course does not care about Debugger.从单独的 powershell 运行该代码将按原样运行,但当然不关心调试器。

piccus皮克斯

将此行添加到您的 launch.json 文件中:

"console": "externalTerminal" 

If you want to use a terminal within VSCode then add this to the configuration block in your launch.json file:如果要在 VSCode 中使用终端,请将其添加到 launch.json 文件中的配置块:

"console": "integratedTerminal"

This will allow you to read input from the console.这将允许您从控制台读取输入。 Your debug points will work too.您的调试点也将起作用。

Make sure that you don't select "internalConsole" in confusion as that won't allow you to read input from a program (refer to the image below)确保您不要混淆选择“internalConsole”,因为这将不允许您从程序中读取输入(请参阅下图)

在此处输入图片说明

What you exactly wanna achieve?你到底想达到什么目的? Do you wanna read line from console and echo back to you?你想从控制台读取一行并回显给你吗? Maybe this will help.也许这会有所帮助。 https://nodejs.org/api/readline.html https://nodejs.org/api/readline.html

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

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