Mendlark / Guides

Vite "Failed to load module script" (MIME type text/html): how to fix it

The page is blank or half loads, and the console says Failed to load module script: Expected a JavaScript-or-Wasm module script but the server responded with a MIME type of "text/html". The browser asked for a JavaScript file and got a web page back instead, usually your index.html or a 404 page. The fix is to find out why that file was not where the page expected it.

Stuck on this? Get it fixed for €49.

Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.

Get it fixed for €49

1. Open the failing file in a new tab

In DevTools → Network, click the red request (something like /assets/index-a1b2c3.js) and open its URL directly. If you see your app's HTML or a "Not found" page, the file does not exist at that address. The next steps explain the usual reasons.

2. An old page is asking for files from the previous deploy

The most common cause right after a redeploy. Vite gives every build new file names, so a tab, a CDN or a service worker still holding the old index.html asks for JavaScript files that no longer exist. Hard-refresh (Ctrl/Cmd+Shift+R) to confirm. To stop it for users: don't let the host cache index.html for long (hashed files under /assets/ can be cached forever), and if you added a PWA plugin, make sure the service worker updates itself or remove it.

// vercel.json: always re-check index.html, cache hashed assets
{
  "headers": [
    { "source": "/index.html", "headers": [{ "key": "Cache-Control", "value": "no-cache" }] },
    { "source": "/assets/(.*)", "headers": [{ "key": "Cache-Control", "value": "public, max-age=31536000, immutable" }] }
  ]
}

3. The base path is wrong

If the app lives in a sub-folder (GitHub Pages at /my-repo/, for example), Vite must know, or it requests /assets/... from the site root. Set base: '/my-repo/' in vite.config. If the app is at the root of its own domain, leave base as '/'; a relative './' base breaks deep links such as /dashboard/settings, because assets are then looked up under /dashboard/.

4. The host is serving your source, not the build

If the failing request is /src/main.tsx, the host published the raw project instead of the built dist folder. Set the build command to npm run build and the output directory to dist. See Vercel "No Output Directory named dist found".

5. A catch-all route answers before your static files

An SPA rewrite should send unknown pages to index.html, never real files. On your own Express or Node server, put app.use(express.static('dist')) before the app.get('*') fallback. On Netlify, use the standard /* /index.html 200 rule (files still win). See blank page after deploying a Vite app for the rewrite setup on each host.

6. A lazy-loaded page fails after a deploy

If it only breaks when someone clicks to a new page (Failed to fetch dynamically imported module), it is the same stale-file problem as step 2. Catch the vite:preloadError event and reload once so users pick up the new build:

window.addEventListener('vite:preloadError', () => {
  if (!sessionStorage.getItem('reloaded')) {
    sessionStorage.setItem('reloaded', '1');
    window.location.reload();
  }
});

Quick checklist

  • Opened the failing file URL and saw HTML, not JavaScript
  • Hard refresh fixes it? Stop long caching of index.html; check any service worker
  • base matches where the app lives (/ for its own domain)
  • Host builds with npm run build and serves dist
  • Static files served before any catch-all route

Stuck on this? Get it fixed for €49.

Fixed in 48 hours or you don't pay. Launch deal for the first 5 customers (normally €79). You get the tested fix as a pull request, so nothing changes until you merge it.

Get it fixed for €49

Still stuck?

Mendlark fixes this kind of problem for a fixed €49 (launch price, normally €79). We agree the problem first, test the fix and send it as a pull request, so nothing changes in your app until you merge it. If we can't fix it, you don't pay. Get the €49 fix, or start with a free scan.

Not sure what else is wrong? Paste your app link into the free instant check and get a plain-English list of problems in seconds.