在我选取的代码中,app.js
在同一范围内包含:
var childProcess1 = require("child_process");
childProcess1.fork(...)
...
var childProcess2 = require("child_process");
childProcess2.fork(...)
有什么理由要求两次吗?
PS:更多详细信息请垂询:)
最佳答案
如果 require
位于不同的函数中,这可能是必要的。像这样:
function foo() {
var childProcess1 = require("child_process");
childProcess1.fork(...)
}
function bar() {
// here you can't use childProcess1, so
var childProcess2 = require("child_process");
childProcess2.fork(...)
}
但最好将代码更改为如下所示:
var childProcess = require("child_process");
function foo() {
childProcess.fork(...)
}
function bar() {
childProcess.fork(...)
}
可能有理由不使用这样的全局变量(但我猜不是内置的 child_process
模块)
关于node.js - 是否有必要多次要求一个模块(在本例中为 child_process)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27690374/