Browser blocks a stylesheet or script: MIME type mismatch with nosniff
The stylesheet or script does not load and the console reports a MIME type mismatch with X-Content-Type-Options: nosniffShort answer
With X-Content-Type-Options: nosniff the browser accepts a stylesheet only as text/css and a script only as a JavaScript type. If your Express app answers the asset URL with an HTML 404 page or a wrong type, the file is blocked. Find what the URL really returns, then fix the static path or route.
Fix it fast
1Find out which cause you have
Request the blocked URL directly and read the status and Content-Type. A stylesheet that returns text/html is usually an error page, not your CSS.
curl -i http://localhost:3000/styles.css | head -n 122Match what you see, then jump to its fix
- 1
curl shows 404 or 200 with Content-Type: text/html for the CSS or JS URLThe file is not found, so the server answers with an HTML pageFix - 2
Every missing asset returns your app's HTML, with a 200 statusA catch-all route sends index.html for every unknown pathFix - 3
curl shows 200 with Content-Type text/plain or application/octet-streamThe file exists but is sent with the wrong Content-TypeFix
- 1
What does the error look like?
The stylesheet or script does not load and the console reports a MIME type mismatch with X-Content-Type-Options: nosniffThe Network tab shows the asset URL answered with Content-Type text/html
Cause 1 of 3
The file is not found, so the server answers with an HTML page
1Check
express.static looks files up relative to the folder you give it, and that folder name is not part of the URL. If the URL and the folder do not line up, the file is not found.
ls public/
grep -n "express.static" server.js2Fix
Make the URL in your HTML match the mounted folder, and use an absolute folder path so the lookup does not depend on where node was started.
const path = require('path');
app.use(express.static(path.join(__dirname, 'public')));
// public/css/site.css is served at /css/site.css3Verify
The URL now answers 200 with text/css.
curl -I http://localhost:3000/css/site.cssConfirmed by expressjs.com
Cause 2 of 3
A catch-all route sends index.html for every unknown path
1Check
A single-page app server often ends with a handler that returns index.html. When express.static does not find a file it passes the request on, and that handler answers instead.
grep -n "index.html" server.js2Fix
Register express.static before the fallback handler, and in the fallback only answer paths that are not asset requests. serve-static also has a fallthrough option; set it to false on a mount that maps to one folder to get an error instead of a silent pass.
app.use('/assets', express.static(path.join(__dirname, 'public/assets'), { fallthrough: false }));3Verify
A missing asset now gives an error, not HTML, and an existing asset still loads.
curl -I http://localhost:3000/assets/missing.css
curl -I http://localhost:3000/assets/site.cssConfirmed by github.com
Cause 3 of 3
The file exists but is sent with the wrong Content-Type
1Check
nosniff stops the browser from guessing. For a style request the browser blocks anything other than text/css, and for a script anything that is not a JavaScript type.
curl -sI http://localhost:3000/css/site.css | grep -i content-type2Fix
Serve the file through express.static, or set the type yourself in a custom route. Do not remove the nosniff header; the fix is the correct type.
app.get('/css/site.css', (req, res) => {
res.type('text/css');
res.sendFile(path.join(__dirname, 'public/css/site.css'));
});3Verify
The header now reads text/css, and the page styles load.
curl -sI http://localhost:3000/css/site.css | grep -i content-typeConfirmed by developer.mozilla.org
How do I stop it coming back?
- Always mount static folders with an absolute path built from __dirname.
- Put API routes and express.static before any catch-all that returns index.html.
- After each deploy, curl one CSS and one JS URL and check status and Content-Type.
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
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 fixBack end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fixBack end
Discord OAuth2 login fails on the redirect_uri in an Express app
invalid_request
Discord compares the redirect_uri you send with the URL you registered for the application, and the token step must repeat the same value as the authorization step.
4 causes, each with a fix