Maaz Zindani

Loading the index…

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

Script fails with MIME type text/html on a nested route in a webpack app

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

  • Applies to webpack 5, webpack-dev-server 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

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

    1. 1The script URL contains the current route, for example /users/bundle.jsoutput.publicPath is relative, so the script URL follows the routeFix
    2. 2Even on the home page, the script request returns HTMLThe dev server serves bundles from a different path than the HTML requestsFix

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

2Fix

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

Confirmed by webpack.js.org

Did this fix it?

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

2Fix

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 serve

Confirmed by webpack.js.org

Did this fix it?

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 fix

Next step

Want this handled for good?

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