Friday 5 May 2017

Node require module can't call parent from submodule

I'm hoping someone can help because I'm kinda tearing my hair out here on this and the other answered questions aren't really what I'm looking for or they don't work.

So basically I have my library split up into 3 separate files, the main file looks like this.

const library = {
  foo: require("./foo")
  bar: require("./bar")
  func: function() {
    console.log("WOOT!");
  }
}

Where foo and bar both look something like this..

module.exports = function(library) {
  return {
    foo_func: function() {
     library.func();
    }
  }
}

So the problem I'm having is that from the library main module I can access the methods within foo and bar like library.foo.foo_func() however foo and bar cannot access the parent library variable. So when calling library.func() i get that library is undefined.

Reading some other suggestions I've tried doing the following where I say

require("./foo")(library) and pass the library to the other modules but this results in an error that tells me require is not a function. I'm at a bit of a loss as to what is wrong with my setup here?



via Austin Smith

No comments:

Post a Comment