webpack cannot find style-loader or another loader
Error: 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.
Fix it fast
1Find out which cause you have
Ask npm whether the loader is installed in this project.
npm ls style-loader css-loader2Match what you see, then jump to its fix
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-loader2Fix
Install both loaders as dev dependencies.
npm install --save-dev style-loader css-loader3Verify
Rebuild; the resolve error should be gone.
npx webpackConfirmed by webpack.js.org
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.js2Fix
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 webpackConfirmed by webpack.js.org
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.js2Fix
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 webpackConfirmed by webpack.js.org
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 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