Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

The browser loads bundle.js, but it contains HTML and fails with Unexpected token '<'

ErrorUncaught SyntaxError: Unexpected token '<'

Short answer

The '<' is the first character of an HTML page. The browser asked for a script URL, got index.html back and tried to run it as JavaScript. Open the Network tab, look at what that URL returns, then fix the script path, output.publicPath or the history fallback so the URL points at the real bundle.

  • 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

    Request the failing script URL yourself. If the first line is <!DOCTYPE html>, the server answered with the page instead of the bundle, and the causes below explain why.

    curl -s http://localhost:8080/bundle.js | head -n 3
  2. 2Match what you see, then jump to its fix

    1. 1The app loads at /, but on a nested route such as /users/42 the script requests /users/bundle.js and gets HTML.A relative script path combined with the history fallbackFix
    2. 2Lazy-loaded chunks or assets fail with the same error while the main bundle loads.output.publicPath does not match where the files are servedFix
    3. 3The script name in index.html differs from output.filename, or the file lives outside static.directory.The dev server does not serve the file the page asks forFix

What does the error look like?

  • Uncaught SyntaxError: Unexpected token '<'
  • bundle.js:1

Cause 1 of 3

A relative script path combined with the history fallback

1Check

historyApiFallback serves index.html in place of 404 responses. A relative src such as bundle.js on a nested route is requested from the wrong folder, gets a 404, and the fallback turns it into HTML.

grep -n -E "<script|historyApiFallback|publicPath" public/index.html webpack.config.*

2Fix

Make the script URL absolute from the site root and set the matching publicPath.

// webpack.config.js
module.exports = {
  output: { filename: 'bundle.js', publicPath: '/' },
  devServer: { historyApiFallback: true },
};

<!-- index.html -->
<script src="/bundle.js"></script>

3Verify

Reload on a nested route and check the Network tab: bundle.js returns JavaScript.

curl -s http://localhost:8080/bundle.js | head -n 1

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 3

output.publicPath does not match where the files are served

1Check

Every emitted file is referenced from output.publicPath, child chunks included. A value that does not match the real hosting path sends chunk requests to the wrong URL.

grep -n "publicPath" webpack.config.*

2Fix

Set the path the app is really served from, or let webpack work it out from the running script.

module.exports = {
  output: { publicPath: 'auto' },
};

3Verify

Open the app, trigger a lazy-loaded route and check that the chunk request returns JavaScript.

# Network tab: filter by JS, open the chunk response

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 3

The dev server does not serve the file the page asks for

1Check

The dev server serves plain files from static.directory (public by default) and bundles from the public path. A name that matches neither is a 404, and then the fallback applies.

grep -n -E "filename|static" webpack.config.*

2Fix

Use the same file name in output.filename and in the script tag, or put the file in the static directory.

module.exports = {
  output: { filename: 'bundle.js' },
  devServer: { static: { directory: path.join(__dirname, 'public') } },
};

3Verify

Request the script URL and confirm it no longer starts with <.

curl -s http://localhost:8080/bundle.js | head -n 1

Confirmed by webpack.js.org

Did this fix it?

How do I stop it coming back?

  • Use absolute script paths and a deliberate publicPath, not defaults you never checked.
  • When you host a single-page app, check that your host serves real files before its index.html fallback.
  • Keep the script name in the HTML and output.filename in one place, ideally generated.

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

Back end

ng serve says it cannot determine the project or target

Cannot determine project for command.

The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.

3 causes, each with a fix

Back end

ng serve cannot find the dev-server builder package

Could not find the implementation for builder @angular/build:dev-server

The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.

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