Skip to content
  1. LuzineDebug
  2. Web, HTML & CSS
  3. Failed to load module script: Expected a JavaScr…
Web, HTML & CSS Intermediate ≈ 15 min to fix

Fix "Failed to load module script: Expected a JavaScript module script … MIME type of text/html" (Vite, SPA)

Error message
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
  • Failed to load module script: Expected a JavaScript-or-Wasm module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
  • Loading module from “https://example.com/assets/index-B2x9kQ.js” was blocked because of a disallowed MIME type (“text/html”).
  • Refused to execute script from 'https://example.com/assets/index.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
  • TypeError: Failed to fetch dynamically imported module: https://example.com/assets/Dashboard-a1b2c3.js
  • Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "application/octet-stream".
In short

The browser requested a JavaScript file (type="module") and the server sent back index.html. On a deployed Vite app, it is almost always the base option not matching the subfolder, assets/ files missing after a new deployment, or a SPA fallback rule returning index.html instead of a 404.

Be the first to confirm this fix

What this error means

For scripts loaded with <script type="module"> or import(), the HTML spec requires a strict check: the response must have a JavaScript MIME type (text/javascript). Single-page apps (React, Vue, Svelte) are often served with a "anything that does not exist returns index.html" rule, so the client-side router works. If the requested JS file does not exist at that URL, the server replies with that HTML page and a 200 code, and the browser refuses to run it. The message talks about MIME, but the real question is: why is the file not at the requested URL?

Common causes

  1. Wrong Vite base: the app is served from a subfolder (/app/, GitHub Pages /repo-name/) but built for /, so it looks for /assets/… at the root.
  2. Old files after a deployment: an open tab or a cached index.html references index-abc123.js files that were replaced by new hashed names.
  3. Source code deployed instead of the build: index.html points to /src/main.tsx, which only the Vite dev server understands.
  4. SPA fallback too broad: try_files … /index.html or an Apache rewrite also applied to missing .js files.
  5. Server without a MIME type for .js or .mjs: an old IIS server, an S3 bucket without Content-Type, which gives application/octet-stream.

Step-by-step fix

  1. Check the script URL

    In DevTools > Network, click the failing JS file: the Response tab shows HTML. Compare the requested URL with the real location of the files on the server.

    bash
    curl -sI https://example.com/assets/index-B2x9kQ.js | grep -iE "^HTTP|content-type"
  2. Set base for a subfolder

    If the app lives under /my-app/, tell Vite and rebuild. With React Router, also pass basename="/my-app" to the router (or import.meta.env.BASE_URL).

    javascript
    // vite.config.js
    import { defineConfig } from 'vite';
    
    export default defineConfig({
      base: '/my-app/',
    });
  3. Deploy the build, not the sources

    Run npm run build and publish the content of dist/ (not the project root). The generated index.html must point to /assets/index-xxxx.js, not to /src/main.

    bash
    npm run build
    grep -o 'src="[^"]*"' dist/index.html
    npx vite preview
  4. nginx: 404 for assets, fallback for routes

    Files under /assets/ must return a real 404 when missing, and index.html must not be cached so a new deployment is picked up immediately.

    nginx
    location /assets/ {
        try_files $uri =404;
        add_header Cache-Control "public, max-age=31536000, immutable";
    }
    
    location / {
        try_files $uri $uri/ /index.html;
        add_header Cache-Control "no-cache";
    }
  5. Apache: only rewrite what does not exist

    In the app folder .htaccess, only send to index.html paths that are neither a file nor a folder. Adjust RewriteBase and the target if the app lives in a subfolder.

    plaintext
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.html [L]
  6. Handle tabs left open during a deployment

    Vite fires the vite:preloadError event when a dynamic import fails: reload the page to get the new index.html. Also keep the old assets/ files for a few days after a deployment if your hosting allows it.

    javascript
    window.addEventListener('vite:preloadError', (event) => {
      event.preventDefault();
      window.location.reload();
    });

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Test each build with npx vite preview, and in the same subfolder as production.
  • Never cache index.html for a long time; only the hashed files in assets/ can be.
  • Use atomic deployments (new folder, then switch a symlink) that keep old assets available for a while.

Frequently asked questions

Why does the error only appear after a new deployment?

The files have new hashed names. An open tab or a cached index.html still requests the old ones, which the server no longer finds: it returns index.html instead.

It works with npm run dev but not once online, why?

The dev server transforms files on the fly and always serves from /. In production, only the files in dist/ exist, at the path set by base.

What base value for GitHub Pages?

For a project site at https://user.github.io/my-repo/, set base: '/my-repo/'. For a user.github.io user site or a custom domain, keep /.

Useful tools for this error

More Web, HTML & CSS errors

All Web, HTML & CSS errors →