1

I'm working on a Discord bot using the popular Discord.js library
I've recently moved all my non config files to a folder called 'src'

In my 'index.js' file I have an fs statement that reads all the files in a directory called './src/handlers'
Here is that code:

fs.readdirSync(`./src/handlers`).forEach((handler) => {
  require(`./src/handlers/${handler}`)(client)
});

Yes, I've imported the 'fs' module.
Here's my button.js code incase it has something to do with the error
(there are more files in the handlers folder, so it's not likely that button.js is giving me the error, it's probably that it's the one at the top of the list)

const fs = require('fs');
const chalk = require('chalk')
var AsciiTable = require('ascii-table')
var table = new AsciiTable()
table.setHeading('Buttons', 'Stats').setBorder('|', '=', "0", "0")

module.exports = (client) => {
    fs.readdirSync('./src/buttons/').filter((file) => file.endsWith('.js')).forEach((file) => {
        const button = require(`../buttons/${file}`)
        client.buttons.set(button.id, button)
        table.addRow(button.id, '✅')
    })
        console.log(chalk.cyanBright(table.toString()))
};

bradys dev
  • 50
  • 7
  • What is the location of your `index.js` file? – Phil Dec 11 '22 at 05:19
  • in the src folder mentioned previously – bradys dev Dec 11 '22 at 05:29
  • Right, I think it's a disconnect between the CWD for `readdir` and `require`. `./` in `require()` is the current script directory but I'm guessing `./` in `readdir` is the process CWD. If `index.js` is in `src`, try `require(\`./handlers/${handler}`)` – Phil Dec 11 '22 at 05:34

0 Answers0