Node.js readline in debug-console in Visual Studio Code
Asked Answered
A

3

9

I'm new to Visual Studio Code, Javascript and Node.js.

Coming from C# I want to debug something like this with breakpoints set in 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. But that ignores my breakpoints.

So, how can I work with readline-functionality and breakpoints set?

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:

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.

piccus

Airiness answered 12/12, 2016 at 12:25 Comment(0)
T
16

Add this line to you launch.json file:

"console": "externalTerminal" 
Thacker answered 23/4, 2018 at 19:33 Comment(2)
that's the good answer, it will use the standard terminal for input and logging, but you can still use visual code for debugging, perfectInsightful
it works well with "console": "integratedTerminal". it will use a vscode terminal - you will not have to switch windows all the timeSnippet
M
10

If you want to use a terminal within VSCode then add this to the configuration block in your launch.json file:

"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)

enter image description here

Muskmelon answered 29/3, 2021 at 7:54 Comment(0)
N
-2

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

Nethermost answered 12/12, 2016 at 12:52 Comment(1)
read the question, readline doesn't work with visual code out of the boxInsightful

© 2022 - 2024 — McMap. All rights reserved.