3

Just for some background I'm creating a discord bot using discord.js. I'm currently following this video guide (linked at the timestamp where I'm having an issue) and I've copied exactly what has been done up until that point.

Here are the versions of everything I'm using:

  • VS Code: 1.65.2
  • Node: 16.14.2
  • npm: 8.5.0
  • TypeScript: 4.6.2
  • dotenv: 16.0.0
  • discord.js: 13.6.0
  • nodemon: 2.0.15

I'll put my code and errors below but also here's a SrcShare: SrcShare Link

index.ts

import DiscordJS, { Intents } from 'discord.js'
import dotenv from 'dotenv'
dotenv.config()

const client = new DiscordJS.Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES
    ],
})

client.on('ready', () => {
    console.log('The bot is ready.')

    const guildId = '276237909378465794'
    const guild = client.guilds.cache.get(guildId)
    let commands

    if (guild) {
        commands = guild.commands
    } else {
        commands = client.application?.commands
    }

    commands?.create({
        name: 'ping',
        description: 'Replies with pong.',
    })
})

client.login(process.env.TOKEN)

Errors I get when running ts-node index.ts in the terminal

return new TSError(diagnosticText, diagnosticCodes)
       ^
TSError: ⨯ Unable to compile TypeScript:
index.ts(22,39): error TS1109: Expression expected.
index.ts(23,5): error TS1005: ':' expected.
index.ts(25,14): error TS1109: Expression expected.
index.ts(29,1): error TS1005: ':' expected.

[nodemon] app crashed - waiting for file changes before starting...

I've tried the solutions from this other stackoverflow post where the problem also seems to be optional chaining with the ? operator. All of the solutions in that thread say you need to update to TypeScript 3.7+ where optional chaining is supported but I'm using TypeScript 4.6.2.

If anyone knows what's going on here I'd appreciate the help!

UPDATE

So I think this might be a problem with nodemon. If I run ts-node index.ts in terminal the bot starts up without errors. But in the package.json I have a script that looks like this "dev": "nodemon index.ts". When I run npm run dev I still get the errors shown above even though nodemon should just be running ts-node index.ts.

Anthony726
  • 75
  • 2
  • 4
  • 10
  • 2
    If it works with `tsc` and successfully transpiles your `ts-node` is probably outdated - use `npm outdated` and check (or just update it anyways to be safe). – iiRealistic_Dev Mar 22 '22 at 07:47
  • I just started getting this error too, and updating `ts-node` did not help. Working on this now, so if I find a solution I'll put up an answer. – Robert Dempsey Mar 27 '22 at 23:27
  • Btw, my problem is not with nodemon, I'm seeing this compilation error on my own code. – Robert Dempsey Mar 28 '22 at 00:20

1 Answers1

3

Check the node_modules folder to see if you are actually using the version of TypeScript you think you're using. My issue was that a package I was depending on was depending on TypeScript 3.4.3 and installing that version in my project's node_modules folder.

Updating the package to one that didn't specify the TypeScript version it relies on fixed it for me.

Robert Dempsey
  • 410
  • 2
  • 12