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?)
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.
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.
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".
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With