webpack stops with 'You may need an additional loader to handle the result of these loaders'
Module parse failed: Unexpected characterShort 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.
Fix it fast
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.*2Match what you see, then jump to its fix
- 1
The 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
Both 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
You 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
The 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
- 1
What does the error look like?
Module parse failed: Unexpected characterYou 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.js2Fix
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 developmentConfirmed by webpack.js.org
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.js2Fix
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 developmentConfirmed by webpack.js.org
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.js2Fix
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 developmentConfirmed by webpack.js.org
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.js2Fix
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 developmentConfirmed by webpack.js.org
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 fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix