Maaz Zindani

Loading the index…

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

The browser cannot get the CSS file from an Express app

ErrorThe page loads without styles, and the network tab shows a 404 for the stylesheet

Short answer

Express serves a CSS file only if express.static points at the folder that holds it, and the URL leaves that folder's name out. A relative folder is resolved from the directory where you started node. Mount express.static with an absolute path and match the link tag to it.

  • Applies to Express 5, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the stylesheet URL straight in the browser address bar. If it shows Cannot GET, the server has no file for that path, and the causes below apply.

    curl -i http://localhost:3000/css/style.css
  2. 2Match what you see, then jump to its fix

    1. 1Every static file returns Cannot GET, not only the CSS fileexpress.static is missing, or points at the wrong folderFix
    2. 2It works with node app.js in the project folder and fails from a service, a script or an IDE runThe folder is relative and node was started from another directoryFix
    3. 3The file exists in the folder, and the URL without the prefix fails while one with the prefix worksA virtual path prefix is used, but the link tag leaves it outFix

What does the error look like?

  • The page loads without styles, and the network tab shows a 404 for the stylesheet
  • Cannot GET /css/style.css

Cause 1 of 3

express.static is missing, or points at the wrong folder

1Check

Express serves no files unless you add the static middleware. It looks the file up relative to the folder you give it, and the folder's own name is not part of the URL.

ls public/css
grep -rn "express.static" --include=*.js . | grep -v node_modules

2Fix

Add the middleware before your routes, and link to the file without the folder name.

app.use(express.static('public'));
// public/css/style.css is now at /css/style.css
// <link rel="stylesheet" href="/css/style.css">

3Verify

The curl command from the first step returns 200 and the CSS text.

curl -i http://localhost:3000/css/style.css

Confirmed by expressjs.com

Did this fix it?

Cause 2 of 3

The folder is relative and node was started from another directory

1Check

A relative static root is resolved against the directory from which you launch node, not against the file that contains the code.

node -e "console.log(process.cwd())"
pwd

2Fix

Build an absolute path from the location of the source file.

const path = require('node:path');
app.use(express.static(path.join(__dirname, 'public')));

3Verify

Start the app from a different folder and request the file again.

cd / && node /path/to/app/app.js

Confirmed by expressjs.com

Did this fix it?

Cause 3 of 3

A virtual path prefix is used, but the link tag leaves it out

1Check

When static is mounted with a path, such as /static, the files are served under that prefix only.

grep -rn "express.static" --include=*.js . | grep -v node_modules

2Fix

Use the prefix in the link, or mount static at the root.

app.use('/static', express.static(path.join(__dirname, 'public')));
// <link rel="stylesheet" href="/static/css/style.css">

3Verify

Request the prefixed URL.

curl -i http://localhost:3000/static/css/style.css

Confirmed by expressjs.com

Did this fix it?

How do I stop it coming back?

  • Use an absolute path for express.static and keep it in one place.
  • Start asset links with a slash, so they do not change when the page URL has more segments.
  • Check the browser's network tab for the exact URL that failed, not the file path on disk.

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

Next step

Want this handled for good?

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