Maaz Zindani

Loading the index…

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

webpack-dev-server shows Cannot GET / in the browser

ErrorCannot GET /

Short answer

The dev server started but has no file for the URL you asked for. It serves bundles from memory and other files from devServer.static, which defaults to the public folder. Put an index.html there that loads your bundle, match the public path, and enable historyApiFallback for client-side routes.

  • Applies to webpack 5, webpack-cli 6, 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 server's own route that shows what it serves, and read the start-up lines for the static directory.

    curl -s http://localhost:8080/webpack-dev-server | head -40
  2. 2Match what you see, then jump to its fix

    1. 1Cannot GET / on the root URL, while the start-up line names a directory that has no index.htmlNo index.html in the static directoryFix
    2. 2The page loads but the script returns 404, or the URL you open differs from output.publicPathThe bundle is requested from a different path than it is served onFix
    3. 3Cannot GET /about only on reload or a direct link; / worksA client-side route is requested directlyFix

What does the error look like?

  • Cannot GET /
  • [webpack-dev-server] Content not from webpack is served from '/app/public' directory

Cause 1 of 3

No index.html in the static directory

1Check

Compare the directory printed by the server with the files in it. The default is public in the folder you started from.

ls public

2Fix

Create an index.html there with a script tag for your bundle, or point static at the folder that has it. webpack-dev-server does not insert script tags for you.

devServer: {
  static: { directory: path.join(__dirname, 'public') },
},

3Verify

Reload the page; the HTML loads.

curl -I http://localhost:8080/

Confirmed by webpack.js.org

Did this fix it?

Cause 2 of 3

The bundle is requested from a different path than it is served on

1Check

The dev server serves bundles at the devServer host and port plus output.publicPath and output.filename, from memory, not from dist.

curl -I http://localhost:8080/main.js

2Fix

Make the script tag match, or set the public path used by the dev middleware.

devServer: {
  devMiddleware: { publicPath: '/' },
},

3Verify

The bundle URL returns 200.

curl -I http://localhost:8080/main.js

Confirmed by webpack.js.org

Did this fix it?

Cause 3 of 3

A client-side route is requested directly

1Check

The server has no file for /about because the router handles it in the browser.

curl -I http://localhost:8080/about

2Fix

Turn on historyApiFallback so index.html is served in place of 404 responses.

devServer: {
  historyApiFallback: true,
},

3Verify

Reload on the route; the app loads.

curl -I http://localhost:8080/about

Confirmed by webpack.js.org

Did this fix it?

How do I stop it coming back?

  • Keep one index.html in the folder devServer.static points to and commit it.
  • Set an absolute path for static.directory instead of relying on the working directory.
  • Add historyApiFallback to any single-page app config from the start.

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.