Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

The browser got an HTML page where a Vite script should be

ErrorFailed 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.

  • Applies to Vite 8, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 5
  2. 2Match what you see, then jump to its fix

    1. 1Works 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. 2Only 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. 3dist works in vite preview but not on the serverThe host is not serving the built dist folderFix

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.html

2Fix

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.html

Confirmed by vite.dev

Did this fix it?

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 index

2Fix

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.html

3Verify

Reload the page and confirm index.html carries the header.

curl -I https://example.com/index.html

Confirmed by vite.dev

Did this fix it?

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 preview

2Fix

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 20

3Verify

The script URL should return a JavaScript Content-Type from the server.

curl -sI https://example.com/assets/index-abc123.js | grep -i content-type

Confirmed by vite.dev

Did this fix it?

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:

Related fixes

Web 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 fix

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.