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"
require
resolves a module; in this casechild_process
nodejs.org/api/modules.html#modules_module_require_id – Izabel