A lazy-loaded chunk will not load in a Vite app
TypeError: Failed to fetch dynamically imported moduleShort answer
The browser asked for a JavaScript chunk and did not get it. After a new deploy the old page still points at chunk names that no longer exist. Other causes are an ad blocker that dislikes the file name, a flaky network, or a dynamic import path Vite cannot resolve. Open the Network tab, find the failed chunk and read its status.
Fix it fast
1Find out which cause you have
Open the browser DevTools Network tab, reload, trigger the lazy route and look at the failed .js request. A 404 or an HTML body points to a stale deploy. A request marked blocked or cancelled points to an extension or the network.
npx vite build && npx vite preview2Match what you see, then jump to its fix
- 1
The error shows up for users who kept the tab open across a release; the chunk URL returns 404A new deploy removed the chunk an open tab still wantsFix - 2
The request in DevTools is shown as blocked by client, and it works in a private window with extensions offAn ad blocker or extension blocks the chunk by its nameFix - 3
It happens on mobile data or a weak connection, and a plain reload fixes itThe network dropped the requestFix - 4
Only imports with a template string or a variable fail, while imports with a plain string workThe import() path is built from a variable in a way Vite cannot followFix
- 1
What does the error look like?
TypeError: Failed to fetch dynamically imported module
Cause 1 of 4
A new deploy removed the chunk an open tab still wants
1Check
Vite names chunks with a content hash. When you publish a new build and delete the old files, a page loaded earlier still holds the old names. Compare the chunk name in the failing request with the files now on the server.
ls dist/assets | head2Fix
Reload the page when a dynamic import fails, using the vite:preloadError event, and serve index.html with Cache-Control no-cache so returning users always get the current entry. Keeping the previous release's chunks on the server for a while also helps.
// main.js
window.addEventListener('vite:preloadError', (event) => {
window.location.reload()
})3Verify
Deploy twice with a tab left open on the first version, then click the lazy route. The page should reload and show the route.
curl -I https://example.com/index.htmlConfirmed by vite.dev
Cause 2 of 4
An ad blocker or extension blocks the chunk by its name
1Check
Blocking is done by file name, so a chunk whose name matches a block list can be caught. Open the app in a profile with no extensions.
ls dist/assets2Fix
Change the chunk naming pattern so names no longer look like blocked words.
// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
build: {
rolldownOptions: {
output: { chunkFileNames: 'assets/chunk-[hash].js' },
},
},
})3Verify
Rebuild, then load the lazy route with the extension switched on.
npx vite buildConfirmed by vite.dev
Cause 3 of 4
The network dropped the request
1Check
Modern browsers can retry a module import that failed on the network, older ones need a page reload. Check whether the failed request is a timeout or a reset rather than a 404.
curl -I https://example.com/assets/chunk-abc123.js2Fix
Use the same vite:preloadError handler as above so the page reloads once instead of staying broken.
window.addEventListener('vite:preloadError', () => {
window.location.reload()
})3Verify
Throttle the network to Offline in DevTools, trigger the route, restore the network and confirm the page recovers.
# DevTools > Network > throttling: Offline, then No throttlingConfirmed by vite.dev
Cause 4 of 4
The import() path is built from a variable in a way Vite cannot follow
1Check
Vite accepts a variable in import() only when the path starts with ./, ../ or #, ends with a file extension, and names a folder one level deep. Look at the failing import().
grep -rn 'import(' src2Fix
Write the path in the allowed form, or list the files with import.meta.glob and pick one from the map.
// allowed
const page = await import(`./pages/${name}.js`)
// or, for more complex cases
const modules = import.meta.glob('./pages/*.js')
const page2 = await modules[`./pages/${name}.js`]()3Verify
Run the build and open each route that uses the dynamic import.
npx vite build && npx vite previewConfirmed by vite.dev
How do I stop it coming back?
- Send Cache-Control no-cache for index.html so the entry file is always fresh.
- Keep the previous release's assets on the server for some time after each deploy.
- Register the vite:preloadError handler once at the app entry.
- Use import.meta.glob instead of building import paths from several variables.
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