Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js console gets closed immediately after i execute the program from Visual Studio 2012 in Windows 8

I have installed Node.js in Windows 8 PC and installed the Node.js plugin for Visual Studio 2012. I executed a basic program from Visual Studio 2012 which just prints a string on console

consol.log("Hi There");

The Node.js console prints "Hi There" and immediately terminates itself. Can anyone provide a solution to fix it?

I have gone through a similar question, is there any other way to fix it apart from using setTimeOut() in the code? (Why does the Node.js scripts console close instantly in Windows 8?)

like image 264
krishna Avatar asked Aug 29 '13 05:08

krishna


People also ask

How do I keep the Output window open in Visual Studio?

Start the project with Ctrl + F5 instead of just F5 . The console window will now stay open with the Press any key to continue . . . message after the program exits.

How do I run node js code in Visual Studio terminal?

You'll need to open a new terminal (command prompt) for the node and npm command-line tools to be on your PATH. To test that you have Node.js installed correctly on your computer, open a new terminal and type node --version and you should see the current Node.js version installed.


2 Answers

From the Debug menu in Visual Studio choose "Options". After this choose "NodeJS Tools" and tick the checkbox "Wait for input when process exits normally".

like image 50
Aleksandar Kostov Avatar answered Sep 28 '22 13:09

Aleksandar Kostov


Instead of directly executing node app.js from Visual Studio, you could instead call a batch file:

wait.bat app.js

Which inside of wait.bat it simply:

node %1
pause press [enter]

or, you could do this on one line, or wrap it in a module or a function:

require('readline')
    .createInterface(process.stdin, process.stdout)
    .question("Press [Enter] to exit...", function(){
        process.exit();
});

It's using an currently marked as "unstable" module of Node to read line input from stdin. It ignores the input though and then closes the process using the exit function.

like image 43
WiredPrairie Avatar answered Sep 28 '22 13:09

WiredPrairie