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

23,028

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

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

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

enter image description here

Solution 4

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

Solution 5

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.

Share:
23,028

Related videos on Youtube

krishna
Author by

krishna

Updated on July 09, 2022

Comments

  • krishna
    krishna almost 2 years

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

  • Marco Medrano
    Marco Medrano about 9 years
    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!
  • naphier
    naphier over 5 years
    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!
  • Simon
    Simon almost 4 years
    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 code
  • derloopkat
    derloopkat almost 4 years
    What do you mean by "previous answer"? The answers are sorted by votes.
  • Rimer
    Rimer over 3 years
    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.
  • Vitaliy Terziev
    Vitaliy Terziev over 2 years
    @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.