javascriptconsolevisual-studio-2017

JavaScript Keep Console Window Open in Visual Studio


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);
    }
});

```


Solution

  • 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.