Home > OS >  ReferenceError get's thrown at me because: Cannot access 'DurationFormatter' before i
ReferenceError get's thrown at me because: Cannot access 'DurationFormatter' before i

Time:10-14

And like the other post I did, I'll get straight to the point. So I get an error because of some sort, but I can see no mistake in my code, so that is why I am here, I am quite weirded out by this. (This is a slash command) Here is the code:

const { version } = require("discord.js");
const { codeBlock } = require("@discordjs/builders");
const { DurationFormatter } = new DurationFormatter();

exports.run = async (client, interaction) => {
  const duration = DurationFormatter.format(client.uptime);
  const stats = codeBlock("asciidoc", `= STATISTICS =
• Men Usage   :: ${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2)}
• Uptime      :: ${duration}
• Users       :: ${client.guilds.cache.map(g => g.memberCount).reduce((a, b) => a   b).toLocaleString()}
• Servers     :: ${client.guilds.cache.size.toLocaleString()}
• Channels    :: ${client.channels.cache.size.toLocaleString()}
• Discord.js  :: v${version}
• Node        :: ${process.version}`);
  await interaction.reply(stats);
};

exports.commandData = {
  name: "stats",
  description: "Show's the bots stats",
  options: [],
  defaultPermissions: true,
};

exports.guildOnly = false;

Here is where the error gets thrown:

ERROR Unhandled rejection: ReferenceError: Cannot access 'DurationFormatter' before initialization
ReferenceError: Cannot access 'DurationFormatter' before initialization
    at Object.<anonymous> (C:\Users\emilb\OneDrive\Desktop\Tap\slash\stats.js:3:31)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at init (C:\Users\emilb\OneDrive\Desktop\Tap\index.js:41:21)
    at Object.<anonymous> (C:\Users\emilb\OneDrive\Desktop\Tap\index.js:63:1)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)

CodePudding user response:

The line

const { DurationFormatter } = new DurationFormatter();

will never work. Here's what that line tells the JavaScript engine to do:

  1. Upon entry the the scope, declare a local constant called DurationFormatter, reserving that identifier in the current scope, without initializing it (which means you can't read its value; it has no value to read yet, not even undefined).
  2. When the step-by-step execution reaches that statement, evaluate the initializer expression new DurationFormatter().
  3. Initialize the constant from step #1 with the value resulting from step #2.

Your code throws an error at step #2 (new DurationFormatter()) because you can't read the value of DurationFormatter (the constant declared in step #1), because it has never been initialized.

  • Related