Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to solve error "SyntaxError: Unexpected token '?'"

I'm not sure what's wrong. I deleted my code and downloaded it then uploaded it again and now I get this error.

Code: https://replit.com/@hi12167pies/webcord#index.js (Click code for code and output for output)

Error:

/home/runner/C8AU9ceLyjc/node_modules/discord.js/src/rest/RESTManager.js:32
    const token = this.client.token ?? this.client.accessToken;
                                     ^

SyntaxError: Unexpected token '?'

I have no idea whats wrong since it's in the node_modules folder.

If you have problems viewing it here is the code:

const http = require("http")
const discord = require("discord.js")
const client = new discord.Client()
const config = require("./config.json")
const fs = require("fs")
// const readLine = require("readline")
// const rl = readLine.createInterface({
//   input: process.stdin,
//   output: process.stdout
// })

let msgs = {
  "873195510251532348": [],
  "873195522633105429": []
}


client.on("ready", () => {
  console.log("ready discord")
})

client.on("message", (message) => {
  if (message.author.bot) return
  if (!config.chats.includes(message.channel.id.toString())) return

  msgs[message.channel.id].push({
    "username": message.author.tag,
    "content": message.content,
    "type": "0"
  })
})

http.createServer((req,res) => {
  const url = req.url.split("?")[0]
  let query = {}
  req.url.slice(req.url.split("").indexOf("?")).slice(1).split("&").forEach((e) => {
    const splited = e.split("=")
    query[splited[0]] = splited[1]
  })

  if (query.q == "messages") {
    let msg = []

    let i = 0
    while (msgs[query.code].length > i) {
      const e = msgs[query.code][msgs[query.code].length - (i+1)]
      msg.push(e)
      i++
    }

    res.write(JSON.stringify(msg))
    res.end()
  } else if (query.q == "post") {
    let name = query.name.split("%20").join(" ")
    let content = query.content.split("%20").join(" ")
    client.channels.cache.get(query.code).send(`**${name}**: ${content}`)
    msgs[query.code].push({
      "username": name,
      "content": content,
      "type": "1"
    })
    res.end()
  } else if (url == "/robot" && query.istrue == "true") {
    res.write("Robot!")
    res.end()
  } else {
    let path
    if (!query.code) {
      path = "./code.html"
    } else {
      if (!config.chats.includes(query.code)) {
        path = "./invaildcode.html"
      } else {
        path = "./chat.html"
      }
    }
    fs.readFile(path, (er, da) => {
      if (er) res.write("Could not get index.html")
      res.write(da)
      res.end()
    })
  }


}).listen(80, (err) => {
  if (err) throw err
  console.log("listening webserver")
})

client.login(process.env.TOKEN)

I am aware my code is not good right now, I am rewriting it but I still want to know what the error is.

like image 578
hi12167pies Avatar asked Nov 25 '25 15:11

hi12167pies


2 Answers

repl.it uses node v12.22.1 but the nullish coalescing operator (??), is relatively new and was added in node v14.

So to use the ?? operator you need to update node in repl.it.

Which you can do by following this repl.it forum post by lukenzy.

Create a file and name it .replit Inside it, copy and paste the following code:

run = """
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh |  bash
export NVM_DIR=\"$HOME/.nvm\"
[ -s \"$NVM_DIR/nvm.sh\" ] && \\. \"$NVM_DIR/nvm.sh\"
[ -s \"$NVM_DIR/bash_completion\" ] && \\.\"$NVM_DIR/bash_completion\"
nvm install 14
node index.js
"""

This will install and use the latest Node.js v14 (14.17.4).
If u want to use a different version, change nvm install 14 to any other number.
Also, change node index.js to the file u want to run.

like image 62
lejlun Avatar answered Nov 28 '25 16:11

lejlun


You need to upgrade to NodeJS version 16 and above for this error to vanish

like image 42
atomicstructure Avatar answered Nov 28 '25 15:11

atomicstructure



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!