webpack reports Module build failed from sass-loader
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):Short answer
The sass-loader line only says which loader failed. The real reason is in the lines under it. The three that cover most cases: no Sass implementation is installed next to the loader, the config still uses legacy API options, or an @use or @import cannot be resolved by webpack.
Fix it fast
1Find out which cause you have
Read the lines directly under the loader line, then list which Sass packages the project really has.
npm ls sass sass-embedded sass-loader webpack npx webpack --mode development 2>&1 | head -402Match what you see, then jump to its fix
- 1
The npm ls output shows sass-loader but neither sass nor sass-embeddedNo Sass implementation is installedFix - 2
The config has api: "legacy" or sassOptions that were written for the old APIThe loader options still use the legacy Sass APIFix - 3
The message under the loader line is a Sass error naming a file or moduleAn @use or @import cannot be resolvedFix
- 1
What does the error look like?
Module build failed (from ./node_modules/sass-loader/dist/cjs.js):
Cause 1 of 3
No Sass implementation is installed
1Check
sass-loader does not bundle the compiler. You install Dart Sass (the sass package) or Sass Embedded yourself, and the loader picks one from your dependencies, trying sass-embedded first and then sass.
npm ls sass sass-embedded2Fix
Install the compiler in the same project as the loader.
npm install sass-loader sass webpack --save-dev3Verify
Run the build again.
npx webpack --mode developmentConfirmed by webpack.js.org
Cause 2 of 3
The loader options still use the legacy Sass API
1Check
The docs say the legacy Sass JS API is no longer supported. Options such as data and url are not available and are ignored, and the option for extra search folders is loadPaths.
grep -n "api:\|includePaths\|data:" webpack.config.js2Fix
Remove api: "legacy", move extra folders to sassOptions.loadPaths, and put prepended code in additionalData.
{
loader: "sass-loader",
options: {
sassOptions: {
loadPaths: [path.resolve(__dirname, "src/styles")],
},
},
}3Verify
Run the build and confirm the Module build failed message is gone.
npx webpack --mode developmentConfirmed by webpack.js.org
Cause 3 of 3
An @use or @import cannot be resolved
1Check
sass-loader hands each @use, @import and @forward request to webpack's resolver. A relative file is tried first, with partial names such as _theme.scss, then the name as written, so aliases and packages from node_modules resolve. Fixed extensions apply: .sass, .scss and .css.
grep -rn "@use\|@import" src | head -202Fix
Correct the path, or add the alias or folder to webpack's resolve so the request can be found.
resolve: {
alias: { "@styles": path.resolve(__dirname, "src/styles") },
},
// in the stylesheet
@use "@styles/theme" as *;3Verify
Rebuild and check that the stylesheet compiles.
npx webpack --mode developmentConfirmed by webpack.js.org
How do I stop it coming back?
- Keep sass and sass-loader in package.json together so both are installed on every machine and in CI.
- Avoid copying loader options from old tutorials; compare them with the current sass-loader page.
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- Qist BazaarA buy-now-pay-later marketplace built from a blank repo, now past 100K downloads on Google Play.
Related fixes
Back end
Angular build fails with SassError: Undefined variable
SassError: Undefined variable.
Sass modules loaded with @use are scoped to the file that loads them and their variables need a namespace prefix.
4 causes, each with a fixBack end
npm install node-sass fails on a current Node.js version
node-sass fails to install on a recent Node.js version
node-sass is end-of-life and its newest release supports Node.js up to 20, so on newer Node.js the install falls back to a source build that fails.
3 causes, each with a fixBack end
Sass cannot find a stylesheet imported with a leading ~
File to import not found or unreadable: ~bootstrap/scss/bootstrap
The ~ prefix is not part of Sass.
3 causes, each with a fixBack end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fix