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

8

20

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

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

Terrify answered 29/8, 2013 at 5:33 Comment(0)
A
21

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

Antimicrobial answered 8/10, 2014 at 17:3 Comment(4)
Right it works, and if you are using setTimeout and you did not saw the result from the function passed to that; this will fix it! thank you!Hydrofoil
So this either no longer works, or I'm doing something else wrong. I only have 3 options under Debug / Options / NodeJs Tools they are Wait for input when process exits abnorrmally, ... exits normally, and enable edit and continue. I have all 3 check, yet F5 still just runs a console window which immediately closes. If I press Ctrl + F5 the console window will stay open. This is under VS 2017 v 15.9.5. Just curious if this is expected or I'm missing something. Thanks!Motivate
I get the same as the comment above, Ctrl + F5 will keep the window open but a regular F5 will close immediately after the last line of codeAnlace
I third this... I have all three checked, and ran CTRL-F5 and F5 versions, both immediately close the execution cmd window on program exit.Horotelic
Y
17

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.

Yount answered 29/8, 2013 at 14:37 Comment(0)
K
11
  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"

enter image description here

Kele answered 6/4, 2016 at 6:51 Comment(1)
I don't have Node.js Tools section in my Options window. (Visual Studio 2022).Pooch
F
6

The previous answer:

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

... Seems to work if you run without debugging.

Debug>Start Without Debugging Ctrl-F5

Fluctuation answered 4/8, 2020 at 16:12 Comment(2)
What do you mean by "previous answer"? The answers are sorted by votes.Ulyanovsk
@DanielManta in my case they are sorted by Active by default I think, anyway pointing to previous answer is not ideal since answers are being shifted as you pointed out.Sergu
C
3

None of these worked for me, so I just set a breakpoint at the last line of code. An awkward solution but does the trick.

Cadency answered 5/7, 2020 at 16:20 Comment(0)
H
1

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

Huskey answered 19/8, 2019 at 7:21 Comment(0)
P
1

Applies to all visual studio versions.

Option 1

 console.log('Hello world');

setTimeout(function () {
    process.exit();
}, 5000);
Propolis answered 25/11, 2019 at 18:56 Comment(0)
I
0

Ive had "C:\Program Files\nodejs\node.exe" checked as "Run this program as an administrator" for an issue. Unchecked the box => fixed

Imparadise answered 22/6, 2023 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.