Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

webpack stops with 'You may need an additional loader to handle the result of these loaders'

ErrorModule parse failed: Unexpected character

Short answer

This message means a loader ran, but what it returned is not something webpack can use as that file's module type. The last loader in a chain must hand back JavaScript (or CSS, JSON or raw content, depending on the type). Check the order of your use array, add the missing last step, or stop two rules handling the same file.

  • Applies to webpack 5, webpack-cli 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the error: it prints the file path and the loaders that already ran on it. Then print the rules that match that file type in your config.

    npx webpack --stats-error-details 2>&1 | head -n 30
    grep -n "test:" webpack.config.*
  2. 2Match what you see, then jump to its fix

    1. 1The failing file is .scss, .less or similar, and the listed loaders end with sass-loader or less-loaderThe last loader in the chain does not return JavaScriptFix
    2. 2Both css-loader and style-loader (or sass-loader and postcss-loader) are present, but the error still showsThe loaders are listed in the wrong orderFix
    3. 3You use file-loader, url-loader or raw-loader and webpack 5 asset modules are also in play, and the error names an image, font or text fileTwo rules handle the same file, so the output is processed twiceFix
    4. 4The file is TypeScript, JSX, CSS or an image and the message names no loader at allThe rule's test does not match, or a loader is missing for a typeFix

What does the error look like?

  • Module parse failed: Unexpected character
  • You may need an additional loader to handle the result of these loaders.

Cause 1 of 4

The last loader in the chain does not return JavaScript

1Check

webpack runs the loaders in a use array from right to left (bottom to top), and the last one to run must return content that matches the module's type. A Sass loader alone returns CSS, which a JavaScript module cannot parse.

grep -n -B2 -A8 "scss" webpack.config.js

2Fix

Put sass-loader at the end of the array (it runs first) and feed its output into the CSS handling, either css-loader plus style-loader or a type that accepts CSS.

module: {
  rules: [
    {
      test: /\.scss$/,
      use: ["style-loader", "css-loader", "sass-loader"],
    },
  ],
},

3Verify

Build again. The file now compiles and the error is gone.

npx webpack --mode development

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 4

The loaders are listed in the wrong order

1Check

Because execution is right to left, a preprocessor written first in the array runs last and hands its output to a loader that expects something else.

grep -n -A6 "use: \[" webpack.config.js

2Fix

Write the array so the loader that must run first is the last element. In the docs' example sass-loader is the last loader to be listed in its chain of two, so it runs first, then postcss-loader.

use: [
  { loader: "postcss-loader" },
  { loader: "sass-loader", options: { sourceMap: true } },
],

3Verify

Rebuild and check that the stylesheet is part of the output.

npx webpack --mode development

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 4

Two rules handle the same file, so the output is processed twice

1Check

Asset modules replace file-loader, url-loader and raw-loader. When an old loader rule stays in the config, the file also gets processed as an asset and the result is processed again.

grep -n -E "file-loader|url-loader|raw-loader|asset/" webpack.config.js

2Fix

Pick one way. Either remove the old loader and use an asset type, or keep the old loader and tell webpack not to re-handle its output by setting the rule type to javascript/auto.

{
  test: /\.(png|jpg|svg)$/,
  type: "asset/resource",
}
// or, to keep a legacy loader:
{
  test: /\.(png|jpg|svg)$/,
  use: "file-loader",
  type: "javascript/auto",
}

3Verify

Rebuild and open an image in the browser to confirm its URL resolves.

npx webpack --mode development

Confirmed by webpack.js.org

Did this fix it?

Cause 4 of 4

The rule's test does not match, or a loader is missing for a type

1Check

A rule applies only when its test (and any include) matches the file path. Print the file extension that failed and compare it with each test regex.

grep -n -E "test:|include:|exclude:" webpack.config.js

2Fix

Add a rule for the type. Images and fonts can use an asset type with no loader. Make sure include does not leave out the folder that holds the file.

{
  test: /\.png/,
  type: "asset/resource",
}

3Verify

Rebuild: the module is now parsed through the rule you added.

npx webpack --mode development

Confirmed by webpack.js.org

Did this fix it?

How do I stop it coming back?

  • Read a use array from the bottom up: the last item runs first.
  • Remove file-loader, url-loader and raw-loader when you move to webpack 5 asset modules.
  • Keep one rule per file type, and use oneOf when a type needs different handling by query.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.