The browser got an HTML page where a Vite script should be
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html".Short answer
The browser requested a script and received HTML, so it refuses to run it. For a Vite build that means the script URL does not point at a real file and the server answered with index.html. The usual reasons are a wrong base path, assets deleted by a newer deploy, or a host that is not serving the dist folder.
Fix it fast
1Find out which cause you have
Open the Network tab, click the failed script and read the Response. If it is your index.html, the URL is wrong or the file is missing on the server. Then repeat the request with curl.
curl -sI https://example.com/assets/index-abc123.js | head -n 52Match what you see, then jump to its fix
- 1
Works at the domain root, breaks under /app/ or on GitHub Pages project sitesThe app is served from a sub-path but base is still '/'Fix - 2
Only returning users or open tabs fail, and the script name has an old hashA new deploy removed the file an old page still requestsFix - 3
dist works in vite preview but not on the serverThe host is not serving the built dist folderFix
- 1
What does the error look like?
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html".
Cause 1 of 3
The app is served from a sub-path but base is still '/'
1Check
Vite builds asset URLs from the base option. For a project site at /<REPO>/ it must be '/<REPO>/'. The generated script src shows what was built.
grep -o 'src="[^"]*"' dist/index.html2Fix
Set base to the sub-path and rebuild.
// vite.config.js
import { defineConfig } from 'vite'
export default defineConfig({
base: '/my-repo/',
})3Verify
Rebuild; the script src should start with the new base.
npx vite build
grep -o 'src="[^"]*"' dist/index.htmlConfirmed by vite.dev
Cause 2 of 3
A new deploy removed the file an old page still requests
1Check
Compare the failing file name with the current contents of the assets folder on the server.
ls dist/assets | grep index2Fix
Serve index.html with Cache-Control no-cache, and keep older assets for a while after a deploy.
# In your host or server settings: Cache-Control: no-cache for /index.html3Verify
Reload the page and confirm index.html carries the header.
curl -I https://example.com/index.htmlConfirmed by vite.dev
Cause 3 of 3
The host is not serving the built dist folder
1Check
vite preview serves the files from dist on port 4173. If it works there, the app is fine and the server setup is wrong, such as a missing assets folder or a rewrite that answers every path with index.html.
npx vite preview2Fix
Upload the whole dist folder, including assets, and review your rewrite rules so a missing asset is not answered with the app shell. Firebase, for example, rewrites ** to /index.html.
npx vite build
ls -R dist | head -n 203Verify
The script URL should return a JavaScript Content-Type from the server.
curl -sI https://example.com/assets/index-abc123.js | grep -i content-typeConfirmed by vite.dev
How do I stop it coming back?
- Test the production build with vite preview before every deploy.
- Set base once per environment and keep it in config, not in the HTML.
- Keep the last release's assets on the server for a short time after switching.
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
Related fixes
Web apps
ChunkLoadError: Loading chunk failed after a Next.js deploy
Missing assets: the client requests JavaScript or CSS files that no longer exist on the server
A browser asked for a JavaScript file that the server no longer has or that another instance never built.
2 causes, each with a fixWeb apps
FATAL ERROR: heap out of memory during a webpack or npm build
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
The build process hit V8's old-space limit and stopped.
3 causes, each with a fixWeb apps
A React component crashes while destructuring props or state
TypeError: Cannot destructure property 'b' of 'null' as it is null.
JavaScript throws this when the right side of a destructuring pattern is undefined or null.
3 causes, each with a fixWeb apps
Jest fails to parse a React Native or Expo dependency
Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax
Jest does not run Babel on node_modules by default, and many React Native packages ship untranspiled source.
3 causes, each with a fixWeb apps
Sentry: Non-Error promise rejection captured with value in a React app
Non-Error exception (or promise rejection) captured with keys: x, y, z
Sentry raises this event when something that is not an Error object is thrown or used to reject a promise, for example a plain object from an API.
3 causes, each with a fixWeb apps
TypeScript says a JavaScript module implicitly has an any type
Could not find a declaration file for module 'react-materialize'. 'path/to/module-name.js' implicitly has an 'any' type.
TypeScript found the JavaScript file but no types for it, and noImplicitAny turns that into an error.
2 causes, each with a fix