Ubuntu 22.04上PM2启动Discord Bot报错:SyntaxError: Unexpected token '.'
环境版本
- PM 2 5.3
- discord.js 14.11
报错信息
SyntaxError: Unexpected token '.'
at wrapSafe (internal/modules/cjs/loader.js:915:16)
at Module._compile (internal/modules/cjs/loader.js:963:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
at Module.load (internal/modules/cjs/loader.js:863:32)
at Function.Module._load (internal/modules/cjs/loader.js:708:14)
at Module.require (internal/modules/cjs/loader.js:887:19)
at Module.Hook._require.Module.require (/usr/local/lib/node_modules/pm2/node_modules/require-in-the-middle/index.js:101:39)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.(/root/ObjectionerBot/node_modules/discord.js/src/index.js:6:22)
at Module._compile (internal/modules/cjs/loader.js:999:30)
/root/ObjectionerBot/node_modules/discord.js/src/client/BaseClient.js:29
userAgentAppendix: options.rest?.userAgentAppendix
^
文件结构

运行的index.js代码
const fs = require('fs'); const { Client, Collection, Events, GatewayIntentBits, EmbedBuilder, SlashCommandBuilder, Partials } = require('discord.js'); const { token, incoming_messages } = require('./config.json'); const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers] }); client.commands = new Collection(); const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); for (const file of commandFiles) { const command = require(`./commands/${file}`); client.commands.set(command.data.name, command); } client.on('guildMemberAdd', (member) => { let welcome = Math.floor(Math.random() * incoming_messages.length) let NewMem = incoming_messages[welcome].replace("${member}", member) const E = new EmbedBuilder() .setTitle("**New Member**") .setDescription(NewMem) .setColor("#A020F0") .setTimestamp() console.log("MEMBER JOINED") member.roles.add([`951942010309345311`]) client.channels.cache.get("952666424436994078").send({ embeds: [E] }); }); client.once('ready', () => { console.log('System is yes!'); const I = new EmbedBuilder() .setColor(`#0a9239`) .setTitle("**RESTART**") .setDescription(`The bot has restarted, this is normal or a update.`) .setTimestamp() client.channels.cache.get("952958463317794826").send({ embeds: [I] }); client.user.setActivity("objectionstudios.com"); }); client.on('interactionCreate', async interaction => { if (!interaction.isCommand()) return; const command = client.commands.get(interaction.commandName); if (!command) return; try { await command.execute(interaction, client); } catch (error) { console.error(error); await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true }); } }); client.on(Events.InteractionCreate, interaction => { if (interaction.isButton()) { console.log(interaction.id) } }); client.login(token);
解决方案
这个错误的核心原因是你的Node.js版本不支持discord.js v14要求的可选链操作符(?.)。discord.js v14明确要求Node.js版本至少为16.9.0,而当前PM2使用的Node.js版本低于这个要求。
修复步骤:
检查当前Node.js版本
在终端执行:node -v若版本低于16.9.0,需要升级Node.js。
升级Node.js到兼容版本
推荐用nvm管理Node.js版本,避免系统版本冲突:# 安装nvm curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash # 重新加载终端配置 source ~/.bashrc # 安装Node.js 18.x(LTS版本,完美兼容discord.js v14) nvm install 18 # 设置为默认版本 nvm alias default 18同步PM2的Node.js版本
重新安装PM2到当前Node.js环境,确保PM2使用新版本:npm install -g pm2 pm2 update重启Bot
pm2 delete index pm2 start index.js
验证
查看PM2日志确认修复:
pm2 logs index
内容的提问来源于stack exchange,提问作者TheProgrammerEthan

