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

656
Views
Discord Bug Error Invalid Token in discord.js

So I want to create a bot discord for fun in JavaScript using discord.js.

I wrote this code:

const Discord = require("discord.js")
const client = new Discord.client()

client.once('ready',() => {
  console.log("Ready !");

});

client.login('token');

But when I'm trying to run it, I get this error:

/home/runner/MelodicPlainApplicationprogrammer/node_modules/discord.js/src/rest/RESTManager.js:32 const token = this.client.token ?? this.client.accessToken; ^ SyntaxError: Unexpected token '?'

This is on repl.it, and when I'm in VSCode, it works.

Why?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

  1. You are using Discord.js v13 and Discord.js13 requires node.js v16 to work. Replit.com uses node.js v12 by default so you will need to set up node16 manually.

First, create a node.js repl enter image description here

Next, run npm install node@16 in shell enter image description here

After that you need to create a file called .replit

enter image description here

Inside the .replit file, add run = "npx node index.js". If your main file has a different name change index.js to your main file's name.

Now when you click run, replit uses node.js v16 instead of v12

  1. Now for the problems with your code:
const Discord = require("discord.js")
const client = new Discord.client()

client.once('ready',() => {
  console.log("Ready !");

});

client.login('token');

In the second line, const client = new Discord.client(), the c in Discord.client has to be capital, so write const client = new Discord.Client()

Now you need to add gateway intents so that your bot receives events. This is required in discord.js13 (Here's a list of gateway intents)

So change const client = new Discord.Client()

to const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] }) (You can add more intents if you want, these are just the basic ones)

You will also need to get your bot token and replace "token" in client.login('token'); with your bot token. Check this to see how to get your token.

Final code:

const Discord = require("discord.js")
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] })

client.once('ready',() => {
  console.log("Ready !");

});

client.login(enter your token here);
about 4 years ago · Juan Pablo Isaza Report

0

Most likely repl.it is running Node 12 or earlier. The error is saying that the operator ?? is a syntax error, meaning that it does not recognize the nullish coalescing operator. It is supported from Node 14.0.0 and later.

about 4 years ago · Juan Pablo Isaza Report

0

Try upgrading your node.js version (go to shell, next to console):

$ npm install node@16

Or you can downgrade your discord.js version to v12.5.3 in your json file, because v12 has way more tutorials than v13 at the moment.

The choice is upto you, you can upgrade your node.js version or downgrade your discord.js version

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!