繁体   English   中英

在node.js中,模块中的一个文件如何查看模块中另一个文件中的功能?

[英]in node.js, how does one file in a module see functions in another file in the module?

在一个模块中,该模块在./modx包含两个文件: modx.jshelper.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM