vue-loader crashes on every .vue file because its major does not match Vue
Your error
Module build failed (from ./node_modules/vue-loader/dist/index.js): TypeError: Cannot read properties of undefined (reading 'styles')4 more lines it can showFix it fast
Most common cause, 1 of 3vue-loader 17 is installed in a Vue 2.7 project
Go back to the legacy loader line, which npm tags as legacy.
npm install --save-dev vue-loader@legacy
// webpack.mix.js (Laravel Mix 6)
mix.js('resources/js/app.js', 'public/js').vue({ version: 2 });Short answer
Module build failed (from ./node_modules/vue-loader/...) with a TypeError on 'styles' or 'parseComponent' of undefined means the loader and Vue do not belong together. vue-loader 17 in a Vue 2.7 project fails on styles; vue-loader 15 without a usable vue-template-compiler fails on parseComponent. Pair vue-loader 17 with Vue 3, or vue-loader 15.10+ with Vue 2.7.
Which cause do you have?
List the installed versions of Vue, the loader and both compilers. The pair decides the cause: loader 17 next to vue 2.7 is cause 1, loader 15 next to vue 2.6 or older is cause 2, loader 15 next to vue 3 is cause 3.
npm ls vue vue-loader vue-template-compiler @vue/compiler-sfcThen find the line you see in this list and open its fix:
- 1vue-loader 17 is installed in a Vue 2.7 project
npm ls shows vue@2.7.x with vue-loader@17, and the error points into vue-loader/dist/index.js and names 'styles' - 2vue-loader 15 on Vue 2.6 or older cannot load vue-template-compiler
npm ls shows vue@2.6.x, the parseComponent error points into vue-loader/lib, and the peer-dependency or version mismatch error appears above it - 3vue-loader 15 is still installed after upgrading to Vue 3
npm ls shows vue@3.x with vue-loader@15, and the parseComponent error follows the peer-dependency error
What does the error look like?
Module build failed (from ./node_modules/vue-loader/dist/index.js): TypeError: Cannot read properties of undefined (reading 'styles')TypeError: Cannot read property 'styles' of undefinedModule build failed (from ./node_modules/vue-loader/lib/index.js): TypeError: Cannot read properties of undefined (reading 'parseComponent')[vue-loader] vue-template-compiler must be installed as a peer dependency, or a compatible compiler implementation must be passed via options.Vue packages version mismatch:
Cause 1 of 3
vue-loader 17 is installed in a Vue 2.7 project
You seenpm ls shows vue@2.7.x with vue-loader@17, and the error points into vue-loader/dist/index.js and names 'styles'
Check
TypeError on 'styles' from vue-loader/dist means the Vue 3 loader is parsing Vue 2 files. vue-loader 17 takes its compiler from vue/compiler-sfc, falling back to @vue/compiler-sfc, and destructures { descriptor, errors } from parse(). Vue 2.7 ships its own vue/compiler-sfc whose parse() returns the descriptor itself, so descriptor ends up undefined and the first read of descriptor.styles throws. The 17.0.0 changelog states the requirement: vue@^3.2.13.
npm ls vue-loaderFix
Go back to the legacy loader line, which npm tags as legacy. With Laravel Mix, also state the Vue version so Mix configures webpack for Vue 2.
npm install --save-dev vue-loader@legacy
// webpack.mix.js (Laravel Mix 6)
mix.js('resources/js/app.js', 'public/js').vue({ version: 2 });Verify
npm ls shows vue-loader 15.11.1, and the build compiles the .vue files.
npm ls vue-loader && npm run buildConfirmed by raw.githubusercontent.com
Cause 2 of 3
vue-loader 15 on Vue 2.6 or older cannot load vue-template-compiler
You seenpm ls shows vue@2.6.x, the parseComponent error points into vue-loader/lib, and the peer-dependency or version mismatch error appears above it
Check
vue-loader 15.10 and later use vue/compiler-sfc only when the installed vue is 2.7 or newer. For older Vue 2 they load vue-template-compiler; when that is missing or its version differs from vue, the loader reports the problem and carries on without a template compiler, so @vue/component-compiler-utils calls parseComponent on undefined.
npm ls vue vue-template-compilerFix
Move to Vue 2.7, the final Vue 2 minor, with vue-loader 15.10 or later; Vue's 2.7 release notes say vue-template-compiler is then no longer needed (keep it only for @vue/test-utils). If you must stay on 2.6, install vue-template-compiler at exactly the vue version.
npm install vue@2.7.16
npm install --save-dev vue-loader@legacy
# staying on 2.6: match the compiler to the installed vue
npm install --save-dev vue-template-compiler@$(node -p "require('vue/package.json').version")Verify
npm ls lists one vue version and, if present, the same vue-template-compiler version; the build passes.
npm ls vue vue-template-compiler && npm run buildConfirmed by raw.githubusercontent.com
Cause 3 of 3
vue-loader 15 is still installed after upgrading to Vue 3
You seenpm ls shows vue@3.x with vue-loader@15, and the parseComponent error follows the peer-dependency error
Check
vue-loader 15 takes the vue/compiler-sfc path only for vue 2.7 or a later 2.x; with Vue 3 it falls back to looking for vue-template-compiler, which is a Vue 2 package. Without it the loader has no compiler, and with a 2.x copy installed the version mismatch error fires. Either way the parse fails on parseComponent.
npm ls vue vue-loaderFix
Install vue-loader 17 (it needs vue 3.2.13 or later) and remove the Vue 2 compiler. With Laravel Mix, set the version to 3.
npm install --save-dev vue-loader@latest
npm uninstall vue-template-compiler
// webpack.mix.js (Laravel Mix 6)
mix.js('resources/js/app.js', 'public/js').vue({ version: 3 });Verify
npm ls shows vue-loader 17.x next to vue 3.x, and the build passes.
npm ls vue vue-loader && npm run buildConfirmed by raw.githubusercontent.com
How do I stop it coming back?
- Keep vue and vue-loader majors pinned side by side in package.json and upgrade them in one commit.
- Commit the lockfile and read vue-loader changes in review, since it often arrives as a dependency of another tool.
- Plan the move off Vue 2: Vue 2.7 was its final minor and Vue 2 reached end of life on 31 December 2023.
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.