Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

webpack reports Module build failed from sass-loader

ErrorModule 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.

  • Applies to webpack 5, sass-loader (current), Dart Sass 1.x, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 -40
  2. 2Match what you see, then jump to its fix

    1. 1The npm ls output shows sass-loader but neither sass nor sass-embeddedNo Sass implementation is installedFix
    2. 2The config has api: "legacy" or sassOptions that were written for the old APIThe loader options still use the legacy Sass APIFix
    3. 3The message under the loader line is a Sass error naming a file or moduleAn @use or @import cannot be resolvedFix

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-embedded

2Fix

Install the compiler in the same project as the loader.

npm install sass-loader sass webpack --save-dev

3Verify

Run the build again.

npx webpack --mode development

Confirmed by webpack.js.org

Did this fix it?

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.js

2Fix

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 development

Confirmed by webpack.js.org

Did this fix it?

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 -20

2Fix

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 development

Confirmed by webpack.js.org

Did this fix it?

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:

Related fixes

Back 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 fix

Back 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 fix

Next step

Want this handled for good?

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