1

I have a same structure of a piece of code and want to access functions defined in other files ...how can i do that tried module.exports & require

app.js

var x= require('./invkFunc1');

x.display1();
x.display2();
x.display3(); 

invkFunc1.js

var x = require('./invkFunc2');

(function(x){
    //var x=  {}
    disp1 = "hello i am from display1"
    x.display1 = function(){
        console.log(disp1)
    }
    return x;
}(x));

invkFunc2.js

var x = require('./invkFunc3');

(function(x){
    //var x=  {}
    x.disp2 = "hello i am from display2"
    x.display2 = function(){
        console.log(x.disp2)
    }
   return x;
}(x));

invkFunc3.js

module.exports = 

(function(){

var x=  {}
    x.disp3 = "hello i am from display3";
    x.display3 = function(){

        console.log(x.disp3)
    }
   return x;
}());

error:

x.display1();
  ^

TypeError: x.display1 is not a function
    at Object.<anonymous> (c:\Users\prabhat.mishra\Desktop\Chrome-DOM-EXT\extension as on 2rd july 2018\SampleExtension\module.exports\app.js:39:3)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3

from all these example i want to see, how can i access the lower level files and make the functionsavailable which is defined in some file to all the files present.
doing this so that i can achieve modularization as before by placing these codes in one file was working perfectly.

Working code: : trying to achieve modularization from above code of below code

var x ={};



(function(){

  //  var x=  {}
        x.disp3 = "hello i am from display3";
        x.display3 = function(){

            console.log(x.disp3)
        }
    }());

    (function(){
      //  var x=  {}
        x.disp2 = "hello i am from display2"
        x.display2 = function(){
            console.log(x.disp2)
        }
        console.log("call in invkFunc2");
       //x.display3();
    }());

(function(){
    //var x=  {}
    x.disp1 = "hello i am from display1"
    x.display1 = function(){
        console.log(x.disp1)
    }
}());

x.display1()
x.display2();
x.display3();

Thanks in advance!

2
  • I think you have to relate them all using encapsulation. Commented Jul 25, 2018 at 9:14
  • It's a huge piece of code restructuring it will take a lot of time..will consider as a last option.. Commented Jul 25, 2018 at 9:19

1 Answer 1

1

if you want use "require",you should exports first,you can code like this

app.js:

var x = require('./invkFunc1');

x.display1();
x.display2();
x.display3();

invkFunc1.js

var x = require('./invkFunc2');
var disp1 = "hello i am from display1"
x.display1 = function(){
  console.log(disp1)
}
module.exports = x;

invkFunc2.js

var x = require('./invkFunc3');
var disp2 = "hello i am from display2"
x.display2 = function(){
  console.log(disp1)
}
module.exports = x;

invkFunc3.js

var x = {}
x.disp3 = "hello i am from display3";
x.display3 = function () {
  console.log(x.disp3)
}
module.exports = x;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.