Home > Mobile >  const Discord = new Discord(); ReferenceError: Cannot access 'Discord' before initializati
const Discord = new Discord(); ReferenceError: Cannot access 'Discord' before initializati

Time:05-31

I got this error im new in discord js can anyone help me in this?

ReferenceError: Cannot access 'Discord' before initialization at Object. (/app/index.js:10:19) at Module._compile (internal/modules/cjs/loader.js:759:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10) at Module.load (internal/modules/cjs/loader.js:628:32) at Function.Module._load (internal/modules/cjs/loader.js:555:12) at Function.Module.runMain (internal/modules/cjs/loader.js:826:10) at internal/main/run_main_module.js:17:11

const { Discord } = require("discord.js");
var fs = require("fs");

const tokens = fs.readFileSync("./tokens.txt", 'utf-8');
const textByLine = tokens.split('\n')
console.log(textByLine)
const clients = [tokens[0], tokens[1]];

for (const token of tokens) {
  const Discord = new Discord();
  Discord.push(Discord);

  Discord.on("ready", () => {
    
    console.log(Discord.user.id);
  });

  Discord.login(token);
}


If i change value's name to another name im getting this error

TypeError: Discord is not a constructor at Object. (/app/index.js:10:18) at Module._compile (internal/modules/cjs/loader.js:759:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:770:10) at Module.load (internal/modules/cjs/loader.js:628:32) at Function.Module._load (internal/modules/cjs/loader.js:555:12) at Function.Module.runMain (internal/modules/cjs/loader.js:826:10) at internal/main/run_main_module.js:17:11

CodePudding user response:

the problem is you are calling the variable the same name as the import. Try naming it something else:

const { Discord } = require("discord.js");

...

const discord = new Discord();

CodePudding user response:

You are attempting to create a new constant with the name of an already existing contsant.

const { Discord } = require("discord.js");

...

for (const token of tokens) {
  const Discord = new Discord();

The last line above is also attempting to define a constant called Discord by trying to instantiate a new instance of the resulting constant! This will never work.

You should rename one of these, the one inside the for loop is going to be the best. JavaScript tends to use variable names with camelCase:

  const discord = new Discord();
  • Related