Maaz Zindani

Loading the index…

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

webpack cannot find style-loader or another loader

ErrorError: Cannot resolve module 'style-loader'

Short answer

webpack loads loaders from node_modules like any module, so this error means the loader package is not installed where webpack looks. Install style-loader and css-loader as dev dependencies, check resolveLoader.modules if you changed it, and list css-loader after style-loader.

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

Fix it fast

  1. 1Find out which cause you have

    Ask npm whether the loader is installed in this project.

    npm ls style-loader css-loader
  2. 2Match what you see, then jump to its fix

    1. 1npm ls prints (empty) for style-loaderThe loader package is not installedFix
    2. 2The package is installed, but the build still cannot find itresolveLoader points somewhere elseFix
    3. 3The build resolves both packages but fails on the CSS contentThe loaders are listed in the wrong orderFix

What does the error look like?

  • Error: Cannot resolve module 'style-loader'
  • Module not found: Error: Can't resolve 'style-loader'

Cause 1 of 3

The loader package is not installed

1Check

Loaders resolve from node_modules after npm install. Naming a loader in the config does not install it.

npm ls style-loader css-loader

2Fix

Install both loaders as dev dependencies.

npm install --save-dev style-loader css-loader

3Verify

Rebuild; the resolve error should be gone.

npx webpack

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 3

resolveLoader points somewhere else

1Check

resolveLoader uses the same options as resolve, but only for loaders. Its modules default is node_modules; a custom value can hide the real folder.

grep -n "resolveLoader" -A4 webpack.config.js

2Fix

Keep node_modules in resolveLoader.modules, or remove the custom setting.

// webpack.config.js
module.exports = {
  resolveLoader: {
    modules: ['node_modules'],
  },
};

3Verify

Rebuild from the project root.

npx webpack

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 3

The loaders are listed in the wrong order

1Check

Loaders in a use array run from right to left, so the last one runs first.

grep -n "css-loader" -B3 webpack.config.js

2Fix

Put style-loader first and css-loader last, so css-loader runs first and style-loader injects its result.

module: {
  rules: [
    { test: /\.css$/i, use: ['style-loader', 'css-loader'] },
  ],
},

3Verify

Rebuild and load a page that imports a .css file.

npx webpack

Confirmed by webpack.js.org

Did this fix it?

How do I stop it coming back?

  • Add the loader to package.json in the same commit that adds it to the config.
  • Run npm ci in CI so a missing dependency fails there first.
  • Leave resolveLoader at its default unless you have a reason.

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.