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

安稳与你 提交于 2019-11-29 18:17:40

问题


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


回答1:


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




回答2:


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.




回答3:


  1. Open the Visual Studio Options property pages from the menu bar with Tools -> Options.
  2. In the menu tree on the left-hand side, select Node.js Tools -> General
  3. Tick the box labelled "Wait for input when process exits normally"




回答4:


One Easy way to track and work may be,

"Right click on project" -> Open Command Promp Here..

and execute the file with node <filename> or project with npm start

Hope this helps..!




回答5:


Applies to all visual studio versions.

Option 1

 console.log('Hello world');

setTimeout(function () {
    process.exit();
}, 5000);


来源:https://stackoverflow.com/questions/18503001/node-js-console-gets-closed-immediately-after-i-execute-the-program-from-visual

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!