Search code examples
javascriptdiscorddiscord.js

loadFiles is not a function discord.js v14 error


I already made loadFiles as a function but this issue still persists.

Code for fileLoader.js:

const { glob } = require('glob');
const { promisify } = require("util");
const proGlob = promisify(glob);

async function loadFiles(dirName){
  const Files = await proGlob(`${process.cwd().replace(/\\/g, "/")}/${dirName}/**/*.js`)
  Files.forEach((file) => delete require.cache[require.resolve(file)]);
  return Files;

module.exports = { loadFiles }

}

Code for eventHandler.js:

async function loadEvents(client) {
  const { loadFiles } = require("../Functions/fileLoader.js");
  const ascii = require("ascii-table");
  const table = new ascii().setHeading("Events", "Status");

  await client.events.clear();

  const Files = await loadFiles("Events");

  Files.foreach((file) => {
    const event = require(file);

    const execute = (...args) => event.execute(...args, client);
    client.events.set(event.name, execute);

    if(event.rest) {
      if(event.once) client.rest.once(event.name, execute);
      else
        client.rest.on(event.name, execute);
    } else {
      if(event.once) client.once(event.name, execute);
      else
        client.on(event.name, execute);
    }

    table.addRow(event.name, "✅")
    
  })  

  return console.log(table.toString(), "\nLoaded Events")
}

Error is the following:

const Files = await loadFiles("Events")
                    ^
TypeError: loadFiles is not a function

And just if anyone asks, I've already made an events folder.


Solution

  • As Elitezen said you can not export a function from within the function. Consider the following revisions:

    fileLoader.js

    const { glob } = require('glob');
    const { promisify } = require("util");
    const proGlob = promisify(glob);
    
    module.exports = async function loadFiles(dirName){
      const Files = await proGlob(`${process.cwd().replace(/\\/g, "/")}/${dirName}/**/*.js`)
      Files.forEach((file) => delete require.cache[require.resolve(file)]);
      return Files;
    }
    

    eventHandler.js

    async function loadEvents(client) {
      const loadFiles = require("../Functions/fileLoader.js");
      const ascii = require("ascii-table");
      const table = new ascii().setHeading("Events", "Status");
    
      await client.events.clear();
    
      const Files = await loadFiles("Events");
    
      Files.foreach((file) => {
        const event = require(file);
    
        const execute = (...args) => event.execute(...args, client);
        client.events.set(event.name, execute);
    
        if(event.rest) {
          if(event.once) client.rest.once(event.name, execute);
          else
            client.rest.on(event.name, execute);
        } else {
          if(event.once) client.once(event.name, execute);
          else
            client.on(event.name, execute);
        }
    
        table.addRow(event.name, "✅")
        
      })  
    
      return console.log(table.toString(), "\nLoaded Events")
    }