28

When spawning a new child in nodejs on windows (child_process.spawn) it always opens a blank console window which stays open until the child process ends.

Is there a way to avoid this?

i.e. we want to run our application as a background service using forever. However, it is not very backgroundy since it keeps opening and closing blank console windows...

EDIT: Making the sub application run in "quiet" mode is not an option since parts of the processes being spawned is wmic.

ronag
  • 49,529
  • 25
  • 126
  • 221
  • 1
    possible duplicate of [How to prevent console from being displayed when using VLC's dummy interface](http://stackoverflow.com/questions/6001034/how-to-prevent-console-from-being-displayed-when-using-vlcs-dummy-interface) – CodeCaster Jul 15 '15 at 15:41
  • The duplicate nor Node's documentation mention a way to do this through your code. Perhaps you can configure the client application through commandline parameters that it shouldn't show a window. If you run Node as a Windows Service you won't see the windows anyway. – CodeCaster Jul 15 '15 at 15:42
  • I use `spawn` and I never get a separate window on windows. Thjo i have listeneras setup for `stdout` and `stderr`. maybe thats the reason (i am not sure)... but I guess it depends on what are you invoking in your spawn. – Gyandeep Jul 15 '15 at 16:01
  • I have the same problem with `exec`. – ronag Jul 15 '15 at 16:03
  • @ronag have you actually tried putting listeners for `stdout` and `stderr` for `data`? I am talking about `spawn`. – Gyandeep Jul 15 '15 at 16:11
  • Yes I have tried that. – ronag Jul 15 '15 at 17:46
  • 1
    I am surprised why the window doesn't come up for me. – Gyandeep Jul 15 '15 at 19:08
  • 3
    The problem is specifically related to forever vs. running node yourself on the command line and leaving it. It only happens with forever for me. spawn and exec both do it. – Brent Jul 20 '15 at 14:55
  • how about using ```cluster```? – syarul Jul 18 '16 at 13:07
  • https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options describes `windowsHide `: "Hide the subprocess console window that would normally be created on Windows systems. Default: false." – Mike Kaganski Aug 05 '21 at 09:28
  • `windowsHide` won't work with the option `detached: true` because of this bug https://github.com/nodejs/node/issues/21825 – Cartucho Dec 02 '21 at 20:32

4 Answers4

1

In 2017, a windowsHide option was introduced:

Hide the subprocess console window that would normally be created on Windows systems. Default: false.

Mike Kaganski
  • 701
  • 6
  • 18
0

This way forever will spawn one console for the app. And not open for each spawn a console window.

forever -c "cmd /c  node" start app.js
RanP
  • 802
  • 7
  • 14
0

Following on from RanP's answer,

forever start --uid "foo" -c "cmd /c node" app.js

You'll need 'start' before your -c args and --uid is optional. Note there is one less space in the -c command, allowing this to work.

0

use detached property like

spawn('node', [filePath, args], {
            detached: true,
            stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
        })
nikita mullick
  • 97
  • 2
  • 11