Home > front end >  Problem exporting module in node: functions not exported correctly
Problem exporting module in node: functions not exported correctly

Time:05-22

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?

CodePudding user response:

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.

  • Related