You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Repl.it平台使用JavaScript开发Discord机器人时遭遇Unexpected token '?'语法错误

Fixing the SyntaxError: Unexpected token '?' in Discord.js

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:


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+):

  1. Uninstall your current Discord.js version:
    npm uninstall discord.js
    
  2. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 04:57:42