What does ("child_process") actually require?

When we call:

var p = require(child_process);

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

To explain my confusion, in the code base I took, I see:

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

I ask myself if it creates another process from a child process or is it childProcess1just a badly run name?

+4
source share
3 answers

, , . . child_process , , . fork(), spawn() ( exec()), ( PID).

, , :

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

API , spawn , .

, , Node , "". , , require(...). , :

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

require("foo") ( ):

var mymodule = require("foo");
var result = mymodule(); // <-- this calls the function returned via module.exports
console.log(result); // "bar"
+12

child_process spawn, exec, execFile fork. require("child_process") , - .

ChildProcess , stdin, stdout pid.

+2

If you doubt what he is doing, check that he is returning.

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

And you can do this (depending on the version of Node.js):

{ 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] }

Latest version of node docs for ChildProcess.

0
source

Source: https://habr.com/ru/post/1569302/


All Articles