How to solve error “SyntaxError: Unexpected token ‘?'”

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.

Leave a Comment