Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
ReferenceError get's thrown at me because: Cannot access 'DurationFormatter' before initialization

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)
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!