Skip to content

Babel Parser Unexpected Token Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Babel Parser Unexpected Token Fix. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

The Problem

Babel throws SyntaxError: Unexpected token when parsing JavaScript files that use modern syntax. Babel's default parser only understands standard JavaScript — it needs presets or plugins for JSX, TypeScript, and modern ECMAScript features.

Quick Fix

Step 1: Install the correct preset for your syntax

# For React JSX
npm install -D @babel/preset-react

# For TypeScript
npm install -D @babel/preset-typescript

# For modern JavaScript
npm install -D @babel/preset-env

Expected output: Presets are installed as dev dependencies.

Step 2: Configure presets in babel.config.js

// babel.config.js — Wrong: no presets
module.exports = { };

// Right
module.exports = {
    presets: [
        '"@babel"/preset-env',
        '"@babel"/preset-react',
        '"@babel"/preset-<a href="/programming-languages/typescript/">TypeScript</a>',
    ],
};

Expected output: Babel parses JSX, TypeScript, and modern JavaScript without unexpected token errors.

Step 3: Add specific plugins for experimental syntax

npm install -D @babel/plugin-proposal-decorators @babel/plugin-proposal-class-properties
module.exports = {
    plugins: [
        ['"@babel"/plugin-proposal-decorators', { legacy: true }],
        ['"@babel"/plugin-proposal-class-properties', { loose: true }],
    ],
};

Expected output: Decorators and class properties parse correctly.

Step 4: Check for missing plugin for pipeline operator or other proposals

// Wrong — pipeline operator |>
const result = input |> double |> toString;

// Need @babel/plugin-proposal-pipeline-operator
module.exports = {
    plugins: [
        ['"@babel"/plugin-proposal-pipeline-operator', { proposal: 'minimal' }],
    ],
};

Expected output: Experimental syntax is parsed correctly.

Step 5: Verify Node.js version matches @babel/preset-env targets

module.exports = {
    presets: [
        ['"@babel"/preset-env', {
            targets: {
                node: 'current', // use current Node version
            },
        }],
    ],
};

Expected output: Babel targets the current Node.js version and only transforms syntax not supported by that version.

Step 6: Clear Babel cache

rm -rf node_modules/.cache/babel-loader

Expected output: Babel re-compiles all files with fresh cache.

Prevention

  • Install @babel/preset-env, @babel/preset-react, and @babel/preset-<a href="/programming-languages/typescript/">TypeScript</a> for modern JS projects
  • Use specific plugins only for experimental syntax you actually use
  • Set browserslist in package.json for consistent preset-env targeting
  • Clear cache after changing Babel configuration

Common Mistakes with parser error

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world BABEL code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between a preset and a plugin?

A preset is a collection of plugins. @babel/preset-env includes all plugins needed for modern JavaScript. @babel/preset-react includes plugins for JSX. Plugins handle individual syntax features. Presets are easier to configure than individual plugins.

Why do I get 'Unexpected token' on optional chaining or nullish coalescing?

These are modern JavaScript features (ES2020). Install @babel/preset-env and ensure targets includes environments that do not support these features. Babel then transforms them automatically.

Can I use Babel with TypeScript without @babel/preset-TypeScript?

No. Babel cannot parse TypeScript syntax without @babel/preset-<a href="/programming-languages/typescript/">TypeScript</a>. Unlike tsc, Babel does not use its own TypeScript parser — it relies on the preset. Install it even if you also use tsc for Type Checking.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro