小编Che*_*e33的帖子

ReferenceError:初始化前无法访问“fs”

我尝试在discordjs bot (v13) 上使用fs,但出现一个奇怪的错误。

在我可以 console.log 我的 fs 对象和错误之间实际上有 3 行:

// Get FS
const fs = require('fs');

// Recieves commands
client.on('interactionCreate', async interaction => {
    console.log(fs); // OK
    switch(interaction.commandName) {
        // Get list of all maps for this server
        case 'maps':
            const pngfiles = fs.readdirSync('./maps/').filter(f => f.endsWith(".png")); // NOT OK !!!!!!!
            response = "**List all maps available in this server:**\n\n\n"+pngfiles.join("\n")+"\n";
            break;
    }
});
Run Code Online (Sandbox Code Playgroud)

和错误:

/home/chenille33/DPW/app.js:156
                const pngfiles = fs.readdirSync('./maps/').filter(f => f.endsWith(".png"));
                                 ^

ReferenceError: Cannot access 'fs' before initialization
    at …
Run Code Online (Sandbox Code Playgroud)

fs node.js discord.js

5
推荐指数
2
解决办法
3万
查看次数

标签 统计

discord.js ×1

fs ×1

node.js ×1