模块导出模块不起作用

时间:2016-08-24 09:30:55

标签: javascript node.js module

我有以下代码需要根据某些条件导出模块,

moduleBase.js
var spawnLinux = require('child-process').spawn;
var module2 = require('module_2');

var isWin = process.platform === 'win32';

module.exports = function spawn() {
    if (isWin) {
        return module_2;
    } else {
        return spawnLinux;
    }
};

问题是module_2在被外部模块使用时返回错误但是如果它在这个特定模块中使用它运行正常,导出中可能出现什么问题?

如果我这样使用它(在不同的模块中)

var module2 = require('module_2');

module2.run(); //this working

这不起作用

var module2 = require('moduleBase);

module2.run();//Here I got error

1 个答案:

答案 0 :(得分:1)

试试这个

var module2 = require('moduleBase)();

module2.run();