【问题标题】:Error: ENOTDIR: not a directory, scandir error on interactionCreate错误:ENOTDIR:不是目录,interactionCreate 上的 scandir 错误
【发布时间】:2022-01-10 07:34:15
【问题描述】:

Commands 中的所有文件都可以正常读取,但我从 'interactionCreate.jslocated inEvents` 收到错误消息

node:internal/fs/utils:343
    throw err;
    ^

Error: ENOTDIR: not a directory, scandir './Events/interactionCreate.js'

我的Event.js文件如下:

const { readdirSync } = require('fs');
const ascii = require('ascii-table');
let table = new ascii("Events");
table.setHeading('EVENTS', ' LOAD STATUS');

module.exports = (client) => {
    readdirSync('./Events/').forEach(dir => {
        const events = readdirSync(`./Events/${dir}`).filter(file => file.endsWith('.js'));
        for(let file of events) {
            let pull = require(`../Events/${dir}/${file}`);
            if(pull.name) {
                client.events.set(pull.name, pull);
            } else {
                table.addRow(file, 'EVENT REGISTERED')
                continue;
            } if(pull.aliases && Array.isArray(pull.aliases)) pull.aliases.forEach(alias => client.aliases.set(alias, pull.name))
        }
    });
    console.log(table.toString());
}

【问题讨论】:

  • 那行代码和那条错误信息不匹配。 ./Commands != ./Events。有些东西你没有给我们看。无论如何,interactionCreate.js 确实不是目录;不要尝试用readdirSync阅读它
  • @mpen,感谢您告诉我。我已经更新了问题,发现这是fs 的问题。

标签: javascript discord.js fs


【解决方案1】:

你的问题在这里:

    readdirSync('./Events/').forEach(dir => {
        const events = readdirSync(`./Events/${dir}`)

readdirSync 将返回 all Events 目录中的条目,包括文件和目录。您已将变量命名为 dir,但它们并不都是目录。错误消息明确指出 ./Events/interactionCreate.js 不是目录。

Events 目录中删除非目录(即移动该文件),或者更好的是,在调用readdirSync 之前检查dir 是否实际上是一个目录。

最简单的方法是添加{withFileTypes: true}选项,然后你可以调用dir.isDirectory()

请参阅文档https://nodejs.org/api/fs.html#fsreaddirsyncpath-options

【讨论】:

    猜你喜欢
    • 2021-12-19
    • 2017-12-29
    • 2022-08-02
    • 2016-10-05
    • 1970-01-01
    • 1970-01-01
    • 2022-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多