Reputation: 1323
I'm working through Basarat Ali Syed's book "Beginning Node.js", and I'm using Visual Studio 2017 (not to be confused with Visual Studio Code). I like the fact that I can view output in a console window that pops up, rather than having to go through the browser.
However, when I run through code like the following, the console window pops up and closes before I have a chance to see the result. I know how to keep the console window open in C# (Console.Read()), but this is JavaScript.
How do I keep the console window open with the following code?
```
function getConnection(callback) {
var connection;
try {
throw new Error('connection failed');
callback(null, connection);
}
catch (error) {
callback(error, null);
}
}
getConnection(function (error, connection) {
if (error) {
console.log('Error:', error.message);
}
else {
console.log('Connection succeeded:', connection);
}
});
```
Upvotes: 2
Views: 4058
Reputation: 21
I'm starting out with JavaScript using Visual Studio 2019 and node.js in Windows 10. Using Ctrl + F5 (Start without debugging) keeps the console open until I press a key.
Note, the "Wait for input when process exits normally" option for Node.js Tools must also be checked.
Upvotes: 2
Reputation: 1323
So it turns out that ctrl + f5 will open a console that stays open. Interestingly, there are two similar questions on stackoverflow about this, here and here, but the "solution" does not work in my Windows 10 / VS 2017 environment. I'll stick with using ctrl + f5, or running node filename.js
in a terminal window.
Upvotes: 2