Jenny M Jenny M - 1 year ago 60
Javascript Question

Module export modules not working

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

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');; //this working

this is not working

var module2 = require('moduleBase);;//Here I got error

Answer Source

try this

var module2 = require('moduleBase)();;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download