{ ^ TypeError: Cannot read properties of undefined" /> { ^ TypeError: Cannot read properties of undefined" /> { ^ TypeError: Cannot read properties of undefined"/>

TypeError: Cannot read properties of undefined (reading 'readdir')

3.8k Views Asked by At

Please help me
the text of the error I get in the terminal:

fs.readdir("./komutlar/",(err,files) => { ^

TypeError: Cannot read properties of undefined (reading 'readdir') at Object. (C:\Users\tDiff\Desktop\TDIFF\tdıff.js:15:4) enter code here

const client = new Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_MEMBERS
    ]
})
const {token} = require("./ayarlar.json")
const {fs} = require("fs");
const { Options } = require("discord.js");
global.client = client;
client.commands = (global.commands = []);

fs.readdir("./komutlar/",(err,files) => {   // where l got the error
    if(err) throw err;  

        files.forEach(f => {
        if(!f.endsWith(".js")) return;
        let p = require(`./komutlar/${f}`)
        
        client.commands.push({
            name:p.name.toLowerCase(),
            description:p.description,
            options:p.options,
            type:p.type,
        });
        console.log(`✔ Komut eklendi: ${p.name}`);
    })
});

fs.readdir("./events/",(err,files) => {  // where l got the error
    files.forEach(f => {
        if(!f.endsWith(".js"))return;
        const e = require(`./events${f}`);
        let eName = f.split(".")[0];
        
        client.on(eName, (...args) => {
            e()
        })
    })
});


client.once("ready",() => {
    console.log(`${client.user.tag} Aktif!`);
    client.user.setPresence({
        activities:[
            {
                name:"TDIFF",
                type:"LISTENING"
            }
        ],
        status:"dnd"
    })
})
client.login(token)```

1

There are 1 best solutions below

1
metodribic On

The error you are getting is actually telling you that you are trying to read/call someting (in your case readdir) on an object/class/... which is undefined.

in your case fs is undefined and the interpreter is trying execute this:

undefined.readdir(...)

which does not make any sense since undefined does not have any properties on its own.

I would assume that you forgot to import the fs package on top of the file:

const fs = require('fs');