ProjectStack
npm

SyntaxError: Unexpected token

Node.js found a character or keyword it didn't expect at that point in the file. This means the JavaScript syntax is invalid — a bracket, comma, or keyword is out of place.

Common causes

  • A missing or extra comma, bracket, parenthesis, or semicolon
  • Using modern JavaScript syntax (optional chaining, nullish coalescing) in an older Node.js version
  • A JSON file has a trailing comma or syntax error and is being required
  • Copy-pasted code with invisible unicode characters or smart quotes

How to fix it

  1. Look at the line and column number in the error and check the code around it
  2. Check for mismatched brackets or parentheses
  3. Upgrade Node.js if using modern syntax: nvm use --lts
  4. Validate JSON files with: node -e "JSON.parse(require('fs').readFileSync('file.json','utf8'))"

Example

SyntaxError: Unexpected token ',' at wrapSafe (node:internal/modules/cjs/loader:1383:16) at Module._compile (node:internal/modules/cjs/loader:1425:27)

Requiring a JSON file that contains a trailing comma after the last property

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.