Script fails with MIME type text/html on a nested route in a webpack app
Refused to execute script from 'http://localhost:8080/users/bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.Short answer
The browser asked for the bundle and the server answered with index.html, so the MIME type is text/html. In a webpack app this happens when publicPath is relative, so the script URL changes with the route, or when the server has no fallback rule for client-side routes. Use an absolute publicPath.
Fix it fast
1Find out which cause you have
Open the Network tab and click the failed script. If its URL contains the route (like /users/) the path is relative; if the URL is right, the response body is index.html for another reason.
curl -I http://localhost:8080/bundle.js2Match what you see, then jump to its fix
What does the error look like?
Refused to execute script from 'http://localhost:8080/users/bundle.js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.
Cause 1 of 2
output.publicPath is relative, so the script URL follows the route
1Check
A relative publicPath is resolved against the HTML page. On a nested route that points at a path that does not exist, and the fallback answers with HTML.
grep -n "publicPath" webpack.config.js2Fix
Use a server-relative path so every route requests the same URL.
// webpack.config.js
module.exports = {
output: {
publicPath: '/',
},
};3Verify
Reload on a nested route and confirm the script URL is /bundle.js (or your output name).
curl -I http://localhost:8080/bundle.jsConfirmed by webpack.js.org
Cause 2 of 2
The dev server serves bundles from a different path than the HTML requests
1Check
The dev server reads publicPath to decide where to serve the output files. If the script tag in index.html points at another path, the request misses and falls through to index.html.
grep -n "publicPath\|<script" webpack.config.js public/index.html2Fix
Make the script URL in the HTML match the output location, or let webpack inject it. Keep historyApiFallback on only so that real page routes, not scripts, get index.html.
// webpack.config.js
module.exports = {
output: { publicPath: '/' },
devServer: { historyApiFallback: true },
};3Verify
Restart the dev server and reload the page.
npx webpack serveConfirmed by webpack.js.org
How do I stop it coming back?
- Keep output.publicPath absolute (/ or a full CDN URL) in every environment.
- Use the same value in the dev server and production build, or set it per environment on purpose.
- In production, configure the web server to return index.html only for page routes, never for .js files.
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
tsconfig rejects allowImportingTsExtensions
An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly
allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.
2 causes, each with a fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix