0

I've taken QUITE some time to track down an issue. I have now found what I think the problem was - but I don't understand how/why it should have been a problem in the first place. It has to do with require(...).

//GeneralManager.js
//Dependancies
const Partie = require('./Partie');
const Joueur = require('./Joueur');
const listParties = []

const GeneralManager = {
  add_partie : function(partie_obj){
   //do stuff
},

  demarrer : function () {

    /*THIS is what I don't get - why doesn't the require above work?*/
    const Partie = require('./Partie')
    listParties.push(new Partie(new Joueur('Albert', 'Ramos', 28, 56, 'Espagne'), new Joueur('Milos', 'Raonic', 28, 16, 'Canada'), '1', 'Hale', '12h30', 0));
    //rest of demarrer metho 
     },

// rest of the object 
 }

module.exports = GeneralManager

//app.js
const GeneralManager = require('./src/GeneralManager')
const gm = GeneralManager
gm.demarrer()

The above works fine. However, if I comment the 2nd import of Partie inside the demarrer function, then I get: TypeError: Partie is not a constructor

How is it so? Do I really have to do multiple require() in each an every function where I need to build that object?

Damian Dziaduch
  • 2,107
  • 1
  • 15
  • 16
logicOnAbstractions
  • 2,178
  • 4
  • 25
  • 37
  • 3
    The code quoted has a syntax error (because you've removed the middle of it, presumably), so it's impossible to say for sure. But fundamentally, if `demarrer` closes over the `Partie` from the `require` call near the top (as it looks like it does), you clearly don't need another `require` of it within `demarrer`; something *else* must be going on. Please update your question with a [mcve] demonstrating the problem. – T.J. Crowder Sep 13 '18 at 15:24
  • 1
    Could it be you have circular dependecies? Take a look at this question: https://stackoverflow.com/questions/10869276/how-to-deal-with-cyclic-dependencies-in-node-js – Roland Starke Sep 13 '18 at 15:29
  • 1
    I assume that this happens because the const Partie isn't part of the exported module. GeneralManager is being used outside of the file itself, so anything that hasn't been exported won't be available to the code that uses the GeneralManager module unless the const Partie is part of the module itself. So the first "const Partie = require('./Partie');" seems redundant to me. – igorshmigor Sep 13 '18 at 15:30
  • A `require()` call has no "scope". The `const Partie` that you are declaring however does have a scope. – Bergi Sep 13 '18 at 15:47
  • Igor - I think you may be right. I had thought that importing it at the top of the document would mean it would be available to that object - it really seems that this isn't the case. If I debug it with require() within the object, I see that Partie() is there under Functions in Webstorm. However, if I only import at the top of the document, it isn't there anymore. Meaning the object doesn't "know" who Partie is anymore... – logicOnAbstractions Sep 13 '18 at 15:47

0 Answers0