The browser cannot get the CSS file from an Express app
The page loads without styles, and the network tab shows a 404 for the stylesheetShort 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.
Fix it fast
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.css2Match what you see, then jump to its fix
- 1
Every static file returns Cannot GET, not only the CSS fileexpress.static is missing, or points at the wrong folderFix - 2
It 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
The 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
- 1
What does the error look like?
The page loads without styles, and the network tab shows a 404 for the stylesheetCannot 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_modules2Fix
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.cssConfirmed by expressjs.com
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())"
pwd2Fix
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.jsConfirmed by expressjs.com
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_modules2Fix
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.cssConfirmed by expressjs.com
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
Back end
Express answers Cannot GET for a page that should exist
Cannot GET /api/users
Cannot GET is Express's default reply when no route or static file matches the request.
4 causes, each with a fixBack end
Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The Network tab shows the asset URL answered with Content-Type text/html
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type.
3 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
Axios call from a React app comes back 404
404 (Not Found)
Status code 404 means a server answered, but it has nothing at that path.
3 causes, each with a fixBack end
bcrypt.hash throws 'data and salt arguments required'
Error: data and hash arguments required
The bcrypt package throws this when hash gets null or undefined for the password or for the salt.
4 causes, each with a fixBack end
bcryptjs rejects hash() or compare() with Illegal arguments
Error: Illegal arguments: string, undefined
bcryptjs checks the types of its two arguments and, when either is wrong, throws Illegal arguments followed by the typeof of the first and the second.
3 causes, each with a fix