The browser loads bundle.js, but it contains HTML and fails with Unexpected token '<'
Uncaught 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.
Fix it fast
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 32Match what you see, then jump to its fix
- 1
The 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
Lazy-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
The 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
- 1
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 1Confirmed by webpack.js.org
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 responseConfirmed by webpack.js.org
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 1Confirmed by webpack.js.org
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 fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack 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 fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix