Jenny M Jenny M - 3 months ago 10
Javascript Question

Module export modules not working

I have the following code which need to export module based of some condition,

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;
}
};


The problem is that module_2 is returning error when used by external module but if it used inside this specific module it runs OK, what can be the problem in the export?

If I use it like this (in diffrent module)

var module2 = require('module_2');

module2.run(); //this working


this is not working

var module2 = require('moduleBase);

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

Answer

try this

var module2 = require('moduleBase)();

module2.run();