what does require("child_process") actually do?
Asked Answered
B

3

9

When we call :

var p = require(child_process);

Are we already creating a child process ? (if not, what is the p here?)

To explain my confusion futher, in a codebase i picked up, i see :

var childProcess1 = require("child_process");
var  _retrieveChild = childProcess1.fork(
           __dirname + '/backgroundProcesses/DadProcess',
           { execArgv: ['--debug=5859'] }
        );

I am asking myself whether it is creating yet another process from a child process or is the childProcess1 just a badly chosed name?

Burnoose answered 29/12, 2014 at 13:43 Comment(2)
require resolves a module; in this case child_process nodejs.org/api/modules.html#modules_module_require_idIzabel
Thanks! and then I have to say, child_process is a poor choice of name.Burnoose
C
17

Requiring modules can sometimes initialize the module, so don't feel bad about not knowing. They are all different. However, child_process does not create a process simply by requiring the module as you have done. You have to call either fork() or spawn() (or exec()) to actually create a new process (and PID).

If you look at the documentation you can see how sometimes they will use this syntax:

var spawn = require('child_process').spawn;
// ...
spawn('ps', ['ax']);

which basically grabs the module API, then the spawn method off of that and aliases it to a local variable for use later in the code.

EDIT

Just to expand on this for your understanding generally, inside of a Node module, the module decides what to "export". Whatever it exports is what will be returned from the require(...) call. For example, if we had this module:

// foo.js
module.exports = function() {
    return "bar";
};

Then require("foo") would give us a function (but it would not have been called yet):

var mymodule = require("foo");
var result = mymodule(); // <-- this calls the function returned via module.exports
console.log(result); // "bar"
Cuticle answered 29/12, 2014 at 13:48 Comment(1)
nice explanation sir.Can u give the solution how to use child process in my daily used API like get the data from mongo or read uploads the images?Epithelium
S
3

The child_process module acts as a namespace for the spawn, exec, execFile, and fork functions. You call require("child_process") to refer to the module, and then call one of its member functions to create a new process.

The return value of each of those functions is a ChildProcess object, which represents the spawned process and has members like stdin, stdout, and pid.

Sideways answered 29/12, 2014 at 13:51 Comment(0)
H
0

If you are in doubt what it does, check what it returns.

var childProcess = require('child_process')
console.log(childProcess); 

And you may get like this (depends on Node.js version):

{ ChildProcess: 
   { [Function: ChildProcess]
     super_: 
      { [Function: EventEmitter]
        EventEmitter: [Circular],
        usingDomains: false,
        defaultMaxListeners: 10,
        init: [Function],
        listenerCount: [Function] } },
  fork: [Function],
  _forkChild: [Function],
  exec: [Function],
  execFile: [Function],
  spawn: [Function],
  spawnSync: [Function: spawnSync],
  execFileSync: [Function: execFileSync],
  execSync: [Function: execSync] }

The latest node version docs for ChildProcess.

Hostler answered 3/12, 2016 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.