1

I've got some serverless functions running on firebase functions, and I'm getting some strange issues which I think maybe relating to how I'm using modules in Node.js

I have a file that deals just with anything to do with a user

users.js

const { updateAudioDoc } = require('./audio')

const getUser(id) => {
   // get user document
}

const addUserStatistics = {
   // some code
   updateAudioDoc()
}

module.exports = {
   getUser
}

I then have another file which just deals with audio

audio.js

...
const { getUser } = require('./users')

const addAudioToUser = id {
   // some code
   getUser(id)
}

module.exports = {
   addUserToAudio
}

With a structure like this, the users.js file is requesting the audio.js file, which in turns requests the users.js.

I'm receiving some errors like getUser() is not a function. I'm wondering if this is because of this way I've structured modules?

HJo
  • 1,902
  • 1
  • 19
  • 30
  • 1
    You've created a circular dependency by the sounds of it (`users` loads `audio` which internally loads `users`) – James Aug 26 '19 at 08:59
  • 1
    It's explained [here](https://nodejs.org/api/modules.html#modules_cycles). You can try to load `require('./users')` in `adudio.js` from `setTimeout`. – rkm Aug 26 '19 at 09:21

0 Answers0