在Repl.it平台使用JavaScript开发Discord机器人时遭遇Unexpected token '?'语法错误
That error is coming from the Discord.js library itself, not your code! The ?? (nullish coalescing) operator is a modern JavaScript feature that your installed Node.js version doesn't support. Here's how to resolve this issue step by step:
Step 1: Check your Node.js version
First, confirm which version of Node.js you're running. Open your terminal and execute:
node -v
Discord.js v13 and newer require Node.js 16.6.0 or higher. If your version is lower than this threshold, you have two actionable solutions:
Option 1: Upgrade Node.js (Recommended)
Upgrading to the latest LTS (Long-Term Support) version of Node.js will fix the syntax error and let you use all the latest features of Discord.js.
- For Windows/macOS: Install the latest LTS version via your preferred method (e.g., official installer or package manager).
- For Linux or using Node Version Manager (nvm):
nvm install --lts nvm use --lts
After upgrading, restart your terminal, verify the new version with node -v, then re-run your bot—it should work without issues.
Option 2: Use an older Discord.js version compatible with your Node.js
If you can't upgrade Node.js right now, install Discord.js v12 (which supports Node.js 12.0.0+):
- Uninstall your current Discord.js version:
npm uninstall discord.js - Install the compatible v12 release:
npm install discord.js@12.5.3
Your existing bot code will work seamlessly with v12 since you're only using basic features like the ready and message events.
Why this error occurs
Discord.js v13+ leverages modern JavaScript features like the ?? operator that aren't supported in older Node.js versions. The error is thrown when Node.js tries to parse this unsupported syntax in the library's internal code.
内容的提问来源于stack exchange,提问作者Saisaaketh Sambana

