[英]in node.js, how does one file in a module see functions in another file in the module?
在一个模块中,该模块在./modx
包含两个文件: modx.js
和helper.js
:
./modx/package.json:
{ "name" : "module x",
"main" : "./modx.js" }
./modx/helper.js:
function subFunc() { }
./modx/modx.js:
exports.mainFunc = function() {
var x = subFunc();
}
我怎么做subFunc()
在helper.js
可见modx.js
当两者都是在modx
模块?
在./modx/helper.js内部
var subFunc = function subFunc() {}
exports.subFunc = subFunc;
内部.modx / modx.js
var helper = require('./helper.js');
exports.mainFunc() {
var x = helper.subFunc();
}
结果是,helper.js中的subFunc函数在外部可用,而modx.js中的mainFunc在外部可用。
从脚本B
可见的脚本A
唯一的对象是module.exports
。 将对象/功能添加到module.exports
(就像使用mainFunc
)使它们从外部可见。 没有别的办法了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.