laravel-mix: Error with Vue lazy loading components: "Failed to resolve async component"
- Laravel Mix Version: 4.0.15
- Node Version: 11.13.0
- NPM Version: 6.7.0
- OS: Ubuntu 16.10
Description:
When using Vue async components, and building my application in development mode (run dev or run watch) I receive the following error.
But after saving any file to trigger another build, everything works perfectly.
[Vue warn]: Failed to resolve async component: function () {
return __webpack_require__.e(/*! import() | components */ "components").then(__webpack_require__.bind(null, /*! ./components/partials/navigation/Navigation.vue */ "./resources/js/app/components/partials/navigation/Navigation.vue"));
}
Reason: TypeError: Cannot read property 'call' of undefined
All JS chunk files are loaded correctly as seen in network debugging.
The Navigation.vue component seems to be the first partial which is included in my base App component.
I’ve splitted all partial Vue components to components.js and router view files to routes.js (using webpackChunkName magic comment with the import function)
Just running run watch / run dev:
Asset Size Chunks Chunk Names
/js/app.js 2.31 MiB /js/app [emitted] /js/app
...
js/components.js 1.11 MiB components [emitted] components
js/routes.js 516 KiB routes [emitted] routes
Saving any file to trigger re-building:
Asset Size Chunks Chunk Names
/js/app.js 2.32 MiB /js/app [emitted] /js/app
...
js/components.js 1.1 MiB components [emitted] components
js/routes.js 513 KiB routes [emitted] routes
js/vendors~components~routes.js 19.5 KiB vendors~components~routes [emitted] vendors~components~routes
What I’ve tried
- Replace all webpack resolve aliases with relative paths
- I stubmled across an issue where the the webpack
[chunkhash]placeholder in theoutput.chunkFilenamewebpack option causes an issue - Modifying webpack
optimization.splitChunks.chunksoption
webpack.mix.js
const mix = require('laravel-mix');
const fs = require('fs');
const webpack = require('webpack');
const tailwind = require('tailwindcss');
const path = require('path');
const glob = require('glob');
const exec = require('child_process').exec;
const pwa = require('sw-precache-webpack-plugin');
mix.webpackConfig({
output: {
chunkFilename: 'js/[name].js',
},
plugins: [
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
],
});
mix.version();
// ...
mix.js('resources/js/app/app.js', 'public/js/app.js');
Loading Vue partial components
Vue.component('navigation', () => import(/* webpackChunkName: "components" */ './components/partials/navigation/Navigation.vue'));
Loading Vue routes
import auth from './auth';
let routes = [{
path: '/',
name: 'home',
component: () => import(/* webpackChunkName: "routes" */ '../components/views/Index'),
}];
export default routes.concat(
auth
);
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 21
- Comments: 40 (2 by maintainers)
Commits related to this issue
- removed style tags due to laravel-mix or webpack bug (https://github.com/JeffreyWay/laravel-mix/issues/2064) — committed to lechatthecat/laravel-coreuivue-integration-by-docker-compose by lechatthecat 5 years ago
I found that it wasn’t working for me because a vendors file containing style-loader and css-loader was not being created when using async imports.
My workaround was to import a blank scss file into my app.js to force style-loader and css loader to be included in the bundle.
Not ideal but it seems to be working now
Update / Workaround (?)
I just noticed that all failing components contain
<style>tags. After removing these everything works normally ondevandproductionbuilds.I was having the same issue with Laravel Mix 4.0. All the failing components contain <style> tags. I was trying to hash my chunks, so my imports look like this:
And the mix options like this:
Downgrading to Laravel Mix 3.0 (and upgrading vue?) appears to have solved it:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
After trying all the recommendations (chunknames, requiring empty scss in app.js, etc.) from this thread and in every case ending unsuccessfully, I finally came to a very interesting conclusion I guess.
Let’s assume in our
app.jsfile we are trying to dynamically import two Vue components (both containing styles) as follows:As we all know, the problem is, that in laravel-mix v4 and v5 these components won’t load and we will get that ugly “failed to resolve” error.
After debugging for hours, I have tested a scenario, when I tried to load the FirstComponent by using the classical import and loading the SecondComponent dynamically. And guess what, the Second Component loaded dynamically without any problems.
So I came up with an idea of creating a dummy Vue component with some dummy styles, that will be imported at the beginning of
app.js. Because this dummy component contains styles and is imported statically, Webpack will trigger the SCSS processing for the dynamically loaded modules as well. This way my real components could be loaded dynamically.The
app.jswill look as follows:Hope this will save someone from debugging all day.
The thread is polluted on non-related or non useful information.
@redisotschek : Have you read the chris-rodgers solution? https://github.com/JeffreyWay/laravel-mix/issues/2064#issuecomment-511364566
Steps:
This will force to load the css-loader, so your Vue styles will be compiled and load.
@romanzipp : I think that is a good idea to close this issue, since we identified the issue and we discovered a workaround.
I am using
"laravel-mix": "^5.0.1", and using dynamic import like below:I was getting this issue only when running
npm run prod(I did not observe this when runningnpm run dev). Also I am not using any<style>in my component.I solved this by following @juliusvdijk advice, i.e., by adding following lines in
webpack.mix.jsAdding the following to my webpack config solved the issue.
thanks @BrandonSurowiec but downgrading laravel mix causes build to fail and introduces other package dependencies with security warnings. I was able to solve my problem by removing laravel-mix from my project.
The problem is in the
scss splittingin Vue and usingmix.scss()both. Laravel mix when having both creates acssfile with manifest js file content in it. which is definitely a bug. which the community mentions a bug from Webpack and will be resolved in webpack 5. But If you use only code splitting in Vue and have the defaultapp.scssfile imported to main Vue component like this(not in scope), so each other component will get the styling properlyand the
webpack.mix.jsfile will have nomix.scssfunction to run only a single app.js file. here is my file.Hope this solves everyone’s question
Just upgraded to the latest version of Mix - 4.0.14. I’m loading some Vue components as above: i.e.
Vue.component('products-grid', () => import('./components/Products/Grid/App.vue'));Initially Mix compiled fine, but the site displayed a console error stating the dynamic component couldn’t be loaded.
After removing 3 lines of scoped styles from
'./components/Products/Grid/App.vuethe site worked fine and all components load dynamically.Failing the above, I’d recommend you downgrade to Mix 3 and wait for a newer version of Mix that uses Webpack 5. See https://laravel-mix.com/docs/4.0/upgrade#notes Dynamic import
I also run into this problem when importing CSS for example:
import 'simplemde/dist/simplemde.min.css';Everything is working fine in watch-mode btw.
I am experiencing the same issue. After upgrading to the latest version of mix and upgrading babel to 7+.
Changing the output does not solve the issue for me.
mix config
babel.rc
The error:
I don’t have style tags in the vue components. So it’s not related to it.
Using
js/chunks/[id].chunk.[chunkhash].jsworks.Source: https://github.com/webpack/webpack/issues/959#issuecomment-524293798
I am already using relative paths.
This is my configuration: Tested on: Mac Os with Node v10.11.0 Ubuntu with Node v8.16.0
This is my package.json
webpack.mix.js
app.js
bad.vue
good.vue
Expected result:
Current result:
It seems that the only difference is that “bad.vue” has styles.
I think that maybe the issue is with vue-loader. I will research a bit more about this issue.
@rferons Downgrade to Laravel Mix 3. My post above fixed it for me.
It’s been a while but, I am having this same exact problem. removing styles does fix the problem for me, however removing the styles from all my components is not a feasible solution to this problem. Will continue to dig, but curious if anyone has found any other solution.