1

I have a module that has an index.js file and 2 different utility files with functions.

Index has:

const utl = require('./tools');
const updt = require('./update');

While both files have a version of (this is the update.js exports):

module.exports = { 
    updateResults, 
    addTeamIDsToArray, 
    updateTeamStats,
    retrieveRankings, 
    numbOfWeeksToToday }

And the tools.js exports:

   module.exports = {
    add_weeks,
    format_date,
    waitForMe, 
    chooseWinner
    }

However, when I check both constants in my index.js file, one of them is empty:

    console.log(updt)
    console.log(utl)

OUTPUT:

    {}
    {
      add_weeks: [Function: add_weeks],
      format_date: [Function: format_date],
      waitForMe: [Function: waitForMe],
      chooseWinner: [Function: chooseWinner]
    }

Why is my update file not exporting correctly?

6
  • Circular dependencies maybe? Commented May 21, 2022 at 21:56
  • Maybe. Update uses Tools to work, and it might have been the problem if the Tools import wasn't working inside Update, and therefore breaking the functions there, but there were no errors. Commented May 22, 2022 at 13:20
  • Was update importing the tools from ./tools or from . (index.js)? Commented May 22, 2022 at 15:21
  • Directly from ./tools. Is that poor form? Commented May 22, 2022 at 15:56
  • Nah, that should work then, unless tools.js imports ./update or ./index. Commented May 22, 2022 at 16:02

1 Answer 1

1

I'm assuming you mean both files have the same module.exports ?

It's impossible to answer this question without seeing whats in the "tools" directory, as it seems to be behaving correctly; returing an object of the contents exported from the file- in this case, an empty object. (Reason: OP added details)

If it's any consolation, I think you're doing this correct overall. This leads me to wonder a few things!

  • Have you tried changing the file name?
  • Using different import methods? (ESM import x from "x" / await import("x"))
  • Create some object random object (we'll call it obj = { updateResults, addTeamIDsToArray, ...}), add all the exported items, then module.exports = obj?
  • Destructuring the import to further debug? Ex: const { updateResults } = reqiure("./tools") ?
  • Obvious but made sure the relative path "./tools" is correct?

These are just a few things to check or try out, just to check.

And last but not least, good ole' turn it off and back on again just to be sure we cover it all.

Sign up to request clarification or add additional context in comments.

2 Comments

Added that to my post. I'm confused because everything seems the same, not sure what could be the bug.
Well, wouldn't you know it, not sure what happened but it is fixed now... Not sure what did it tbh, but I couldn't have done it without your requests for additional info + all of the really good ideas you suggested. Thanks =)

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.