Fix "Failed to load module script: Expected a JavaScript module script … MIME type of text/html" (Vite, SPA)
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".
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.
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
- 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. - Old files after a deployment: an open tab or a cached
index.htmlreferencesindex-abc123.jsfiles that were replaced by new hashed names. - Source code deployed instead of the build:
index.htmlpoints to/src/main.tsx, which only the Vite dev server understands. - SPA fallback too broad:
try_files … /index.htmlor an Apache rewrite also applied to missing.jsfiles. - Server without a MIME type for
.jsor.mjs: an old IIS server, an S3 bucket withoutContent-Type, which givesapplication/octet-stream.
Step-by-step fix
-
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.
curl -sI https://example.com/assets/index-B2x9kQ.js | grep -iE "^HTTP|content-type" -
Set base for a subfolder
If the app lives under
/my-app/, tell Vite and rebuild. With React Router, also passbasename="/my-app"to the router (orimport.meta.env.BASE_URL).// vite.config.js import { defineConfig } from 'vite'; export default defineConfig({ base: '/my-app/', }); -
Deploy the build, not the sources
Run
npm run buildand publish the content ofdist/(not the project root). The generatedindex.htmlmust point to/assets/index-xxxx.js, not to/src/main.npm run build grep -o 'src="[^"]*"' dist/index.html npx vite preview -
nginx: 404 for assets, fallback for routes
Files under
/assets/must return a real 404 when missing, andindex.htmlmust not be cached so a new deployment is picked up immediately.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"; } -
Apache: only rewrite what does not exist
In the app folder
.htaccess, only send toindex.htmlpaths that are neither a file nor a folder. AdjustRewriteBaseand the target if the app lives in a subfolder.RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L] -
Handle tabs left open during a deployment
Vite fires the
vite:preloadErrorevent when a dynamic import fails: reload the page to get the newindex.html. Also keep the oldassets/files for a few days after a deployment if your hosting allows it.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.htmlfor a long time; only the hashed files inassets/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
Error finder
Paste an error message or a log: the tool recognises the error and opens the right fix.
HTTP status codes
All the common HTTP codes (401, 403, 404, 419, 429, 500, 502…): meaning, causes and what to do.
cURL converter
Paste a cURL command (DevTools “Copy as cURL”): get the fetch, axios, Node, PHP (Laravel Http or curl) or Python requests code.
More Web, HTML & CSS errors
- Fix "Mixed Content: … requested an insecure resource" (HTTPS mixed content)
- Fix "Refused to apply style … MIME type ('text/html') is not a supported stylesheet MIME type"
- Fix "Refused to execute inline script because it violates the following Content Security Policy directive"
- Fix ERR_TOO_MANY_REDIRECTS "redirected you too many times" (redirect loop)
- Fix "Your connection is not private" NET::ERR_CERT_DATE_INVALID and ERR_CERT_AUTHORITY_INVALID
- Fix "ResizeObserver loop completed with undelivered notifications"
- Fix net::ERR_BLOCKED_BY_CLIENT (ad blocker, analytics, "ads" files)