Node: A shell is spawning every time we spawn a child process on Windows

Created on 11 Jul 2016  路  12Comments  路  Source: nodejs/node

  • Version: from 0.10 to 6.2
  • Platform: Windows

When spawning a child process with `require('child_process').spawn, a shell spawn for few milliseconds (almost one seconds) on Windows.
pm2's users are reporting us this issue (here or here) , which come from this code that we are using to spawn the user app.
Just asking why it is spawning and if we can prevent them to spawn ?

Thanks !

child_process question windows

Most helpful comment

We are using childProcess.fork().
When we run "node myapp.js" from the command line, everything is fine.
When we run "pm2 start myapp.js", cmd windows pop up everywhere.

I find it hard to believe people aren't having major problems with this. The pm2 behavior does not mimic the node behavior. Trying to push a production app onto a windows device is no longer possible with pm2 as continual popups are not an option.

Whether it is a bug or not is inconsequential. The point is: there has to be a way around this problem.
Thanks

All 12 comments

See https://nodejs.org/dist/latest-v6.x/docs/api/child_process.html#child_process_child_process_spawn_command_args_options -- make sure the shell option is false.

Let me know if it is false but still spawning a shell. I'm unsure but it could be necessary. Or, it may be how spawn() has to work on windows, exec() may not have the issue. Not sure.

As you can see here, and as the documentation tell Defaults to false (no shell), we spawn with the shell set to false.
So no, its still spawning a shell, i will try asap with exec (as i remember its spawning a shell too, but not sure, i will re-test)

When spawning a child process with `require('child_process').spawn, a shell spawn for few milliseconds (almost one seconds) on Windows.

When you say 'spawns a shell', do you mean it opens a console window?

@bnoordhuis Yes

Right. That's a property of the executable you are spawning and not something node.js has direct control over. Closing, not a bug.

@bnoordhuis We are spawning the node executable by default like you can see here when spawning an app, same behavior happening here, but no console window popup with npm here.
So i believe this behavior is linked to node, could you verify that it doesnt come from node itself ?

You aren't running npm with detached. That could be why.

It's a flag in the PE header of the .exe that controls whether a console window is opened or not. IOW, whether an application is a console application or not is something that is decided at compile time.

You can suppress it up to a point by passing { detached: true } to spawn() but there is nothing stopping the child process from creating a console window manually or spawning children that create one.

We are using childProcess.fork().
When we run "node myapp.js" from the command line, everything is fine.
When we run "pm2 start myapp.js", cmd windows pop up everywhere.

I find it hard to believe people aren't having major problems with this. The pm2 behavior does not mimic the node behavior. Trying to push a production app onto a windows device is no longer possible with pm2 as continual popups are not an option.

Whether it is a bug or not is inconsequential. The point is: there has to be a way around this problem.
Thanks

@wayofthefuture did you ever find a solution to your issue? I seem to have the same problem. Straight running the app using "node app.js" works but running the app in pm2 spawns consoles.

Yup! Here's the solution:

https://github.com/kohsuke/winsw

I ran into problems with pm2 in both linux and windows. So now I use systemd and winsw and no longer use pm2, just straight up node myapp.js using a service... not to mention the permission problems and startup problems involved with cross-platform implementation of pm2.

Looks like someone has opened a new issue (https://github.com/nodejs/node/issues/15217) and a solution has been found, just need to implement it

Was this page helpful?
0 / 5 - 0 ratings

Related issues

substack picture substack  路  878Comments

Trott picture Trott  路  87Comments

thecodingdude picture thecodingdude  路  158Comments

VanCoding picture VanCoding  路  204Comments

yury-s picture yury-s  路  89Comments