【问题标题】:Requiring a folder and using exports.{function}需要一个文件夹并使用导出。{function}
【发布时间】:2017-03-18 19:06:17
【问题描述】:

目前我有这个设置:

// index.js
var example = require('./folder');

和:

// folder/index.js
require('./more');

// folder/test.js
exports.thing = function() {
    console.log('test');

    return true;
}

但是当我尝试在index.js 中调用example.thing 时,我得到:

example.thing is not a function

有什么方法可以让它工作吗?干杯。

【问题讨论】:

    标签: node.js requirejs require


    【解决方案1】:

    节点不支持需要的目录。如果它出现在目录中,则需要index.js。

    要在index.js 中导出thing,请执行以下操作:

    // index.js
    exports.thing = require('./test.js').thing;
    

    【讨论】:

      【解决方案2】:

      你说对了吗?因为我试过了,而且效果很好。

      您应该将其命名为 example.thing() 而不是 example.thing。

      【讨论】:

        猜你喜欢
        • 2018-09-22
        • 1970-01-01
        • 2015-12-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-18
        相关资源
        最近更新 更多