title: How do I resolve TypeScript optional chaining error "TS1109: Expression expected" in VS Code?
body: <p>Just for some background I'm creating a discord bot using discord.js. I'm currently following this <a href="https://youtu.be/pXehoXnFxPM?t=235" rel="nofollow noreferrer">video guide</a> (linked at the timestamp where I'm having an issue) and I've copied exactly what has been done up until that point.</p>
  <p>Here are the versions of everything I'm using:</p>
  <ul>
  <li>VS Code: 1.65.2</li>
  <li>Node: 16.14.2</li>
  <li>npm: 8.5.0</li>
  <li>TypeScript: 4.6.2</li>
  <li>dotenv: 16.0.0</li>
  <li>discord.js: 13.6.0</li>
  <li>nodemon: 2.0.15</li>
  </ul>
  <p>I'll put my code and errors below but also here's a SrcShare: <a href="https://srcshare.io/?id=62391987237093507ca1a16c" rel="nofollow noreferrer">SrcShare Link</a></p>
  <p><strong>index.ts</strong></p>
  <pre><code>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', () =&gt; {
      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)
  </code></pre>
  <p><strong>Errors I get when running <code>ts-node index.ts</code> in the terminal</strong></p>
  <pre><code>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...
  </code></pre>
  <p>I've tried the solutions from this other <a href="https://stackoverflow.com/questions/54611471/typescript-optional-chaining-error-expression-expected-ts1109">stackoverflow post</a> where the problem also seems to be optional chaining with the <code>?</code> 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.</p>
  <p>If anyone knows what's going on here I'd appreciate the help!</p>
  <p><strong>UPDATE</strong></p>
  <p>So I think this might be a problem with nodemon. If I run <code>ts-node index.ts</code> in terminal the bot starts up without errors. But in the <code>package.json</code> I have a script that looks like this <code>&quot;dev&quot;: &quot;nodemon index.ts&quot;</code>. When I run <code>npm run dev</code> I still get the errors shown above even though nodemon should just be running <code>ts-node index.ts</code>.</p>