javascript 在 Windows 8 中从 Visual Studio 2012 执行程序后,Node.js 控制台立即关闭
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18503001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js console gets closed immediately after i execute the program from Visual Studio 2012 in Windows 8
提问by krishna
I have installed Node.js in Windows 8PC 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
我已经在Windows 8PC 中安装了 Node.js 并为Visual Studio 2012安装了 Node.js 插件。我从 Visual Studio 2012 执行了一个基本程序,它只是在控制台上打印一个字符串
consol.log("Hi There");
The Node.js console prints "Hi There" and immediately terminates itself. Can anyone provide a solution to fix it?
Node.js 控制台打印“ Hi There”并立即自行终止。任何人都可以提供解决方案来解决它吗?
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?)
我遇到了一个类似的问题,除了在代码中使用setTimeOut()之外,还有其他方法可以解决它吗?(为什么 Node.js 脚本控制台会在 Windows 8 中立即关闭?)
回答by Aleksandar Kostov
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".
从 Visual Studio 的调试菜单中选择“选项”。在此之后选择“NodeJS 工具”并勾选“进程正常退出时等待输入”复选框。
回答by WiredPrairie
Instead of directly executing node app.js
from Visual Studio, you could instead call a batch file:
node app.js
您可以调用批处理文件,而不是直接从 Visual Studio执行:
wait.bat app.js
Which inside of wait.bat
it simply:
它里面的wait.bat
简单:
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.
它使用当前标记为“不稳定”的 Node 模块来读取来自stdin
. 它会忽略输入,然后使用该exit
函数关闭进程。
回答by Xharlie
- Open the Visual Studio Optionsproperty pages from the menu bar with Tools -> Options.
- In the menu tree on the left-hand side, select Node.js Tools -> General
- Tick the box labelled "Wait for input when process exits normally"
- 使用Tools -> Options从菜单栏中打开 Visual Studio Options属性页。
- 在左侧的菜单树中,选择Node.js Tools -> General
- 勾选标记的盒子“等待输入时处理正常退出”
回答by Vivek Ananthan
One Easy way to track and work may be,
一种跟踪和工作的简单方法可能是,
"Right click on project" -> Open Command Promp Here.
.
"Right click on project" -> Open Command Promp Here.
.
and execute the file with node <filename>
or project with npm start
并执行文件node <filename>
或项目npm start
Hope this helps..!
希望这可以帮助..!
回答by clleker
Applies to all visual studio versions.
适用于所有 Visual Studio 版本。
Option 1
选项1
console.log('Hello world');
setTimeout(function () {
process.exit();
}, 5000);