需要来自单个JS文件与Node / Component.js的多个子模块
我有一个单独的JS文件(以myDependency作为示例,在私人github回购中)。
我已经将几个子模块移动到此回购,并希望在我的应用程序中单独要求这些。
到目前为止,我有以下(所有示例名称):
“myDependency”:
index.js /
module.exports = function () { require(".first"); require(".second"); require(".third"); require(".fourth"); require(".fifth"); require(".sixth"); require(".seventh"); require(".eighth"); require(".ninth"); };
在我的component.json中,我想要做以下事情:
dependencies: { "privateRepoName/myDependency": "master" }
然后在我的脚本中:
var first = myDependency.first;
这不起作用。 我在第一次没有访问的方法。 不知道我在做什么错。
require
函数的结果不会暴露在index.js中。 实际上, myDependency
是一个函数对象,调用时不返回任何东西。
要解决这个问题,使用所有必需的模块导出一个对象,像这样
module.exports = { "first" : require("./first"), "second" : require("./second"), "third" : require("./third"), "fourth" : require("./fourth"), ... }
请注意,path应以./
开头,而不是.
。