Skip to content
Latchkey

webpack "Module parse failed: Unexpected token" - Need a Loader

webpack only understands plain JavaScript on its own. When it meets JSX, TypeScript, modern syntax, or a non-JS file with no matching loader, it cannot parse it and stops with "Unexpected token".

What this error means

The build fails with Module parse failed: Unexpected token and the tell-tale line "You may need an appropriate loader to handle this file type." It points at JSX, TS, or a CSS/asset import that no rule covers.

webpack
ERROR in ./src/Button.jsx 5:9
Module parse failed: Unexpected token (5:9)
You may need an appropriate loader to handle this file type, currently
no loaders are configured to process this file.
>   return <button>Click</button>;

Common causes

No loader for JSX/TS syntax

JSX or TypeScript needs babel-loader, ts-loader, swc-loader, or esbuild-loader. Without a rule matching the file, webpack parses it as plain JS and chokes on < or a type annotation.

A loader excludes the file

A rule exists but its include/exclude skips the file - commonly exclude: /node_modules/ when a dependency ships untranspiled ESNext that needs transpiling.

Non-JS asset imported without a rule

Importing .css, .svg, or similar with no css-loader/asset-module rule makes webpack parse the file contents as JavaScript.

How to fix it

Add a loader rule for the syntax

Configure a transpiler for JSX/TS files and rules for assets.

webpack.config.js
// webpack.config.js
module: {
  rules: [
    { test: /\.[jt]sx?$/, exclude: /node_modules/, use: 'babel-loader' },
    { test: /\.css$/, use: ['style-loader', 'css-loader'] },
  ],
}

Stop excluding a dependency that needs transpiling

When a node_modules package ships untranspiled code, narrow the exclude so it is processed.

webpack.config.js
{ test: /\.m?js$/, include: /node_modules\/some-esnext-pkg/, use: 'babel-loader' }

How to prevent it

  • Define loader rules for every file type you import (JS/TS/JSX, CSS, assets).
  • Be deliberate about exclude: /node_modules/ - some deps ship untranspiled.
  • Keep your Babel/TS preset list aligned with the syntax features you use.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →