WebAssembly compile fails: Incorrect response MIME type, expected application/wasm
TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'Short answer
The streaming WebAssembly functions only accept a response that is OK, same-origin and has the exact type application/wasm. In Express the usual reasons are a wrong path answered by an index.html fallback, a route that sends the file as a Buffer, or a Content-Type with extra parameters. Check the real header with curl.
Fix it fast
1Find out which cause you have
Request the exact URL the page fetches and read its status and Content-Type. The two lines decide the cause: HTML means the wrong response, octet-stream means a route sent raw bytes, and application/wasm followed by more text means an extra parameter.
curl -sI http://localhost:3000/pkg/app.wasm | grep -iE '^HTTP|^content-type'2Match what you see, then jump to its fix
- 1
The response body is an HTML page, not the binary fileThe URL is wrong and a fallback route answers with index.htmlFix - 2
The response type is application/octet-streamA route sends the file as a raw BufferFix - 3
The header is application/wasm followed by more text, such as a charsetThe Content-Type carries extra parametersFix - 4
The status is 404 or 500, or the .wasm file lives on a different hostThe response is not OK or comes from another originFix
- 1
What does the error look like?
TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'The .wasm request in the Network tab returns HTML, or a Content-Type other than application/wasm
Cause 1 of 4
The URL is wrong and a fallback route answers with index.html
1Check
express.static does not send a 404 for a file it cannot find: it calls next() and lets the next middleware answer. A catch-all that returns index.html (common in single-page apps) then answers the .wasm URL with your HTML page. Compare the path the page requests with the folder you serve.
curl -s http://localhost:3000/pkg/app.wasm | head -c 80
ls public/pkg/2Fix
Correct the path (the file must sit under the static root, at the URL the script requests), and keep the fallback from answering URLs that have a file extension. If the header is still not application/wasm afterwards, set it as in the third cause.
const path = require('node:path');
app.use(express.static(path.join(__dirname, 'public')));
// fallback for page routes only
app.use((req, res, next) => {
if (path.extname(req.path)) {
return next(); // a missing file stays a 404
}
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});3Verify
The wasm URL now returns status 200 and the file's own bytes, not HTML.
curl -sI http://localhost:3000/pkg/app.wasm | grep -iE '^HTTP|^content-type'Confirmed by expressjs.com
Cause 2 of 4
A route sends the file as a raw Buffer
1Check
When res.send() gets a Buffer it sets Content-Type to application/octet-stream unless one was set before. A route that reads the .wasm file with fs and passes the bytes to res.send() ends up here.
grep -rn "\.wasm" --include="*.js" --exclude-dir=node_modules .2Fix
Set the type before sending. res.type() accepts the full MIME type and uses it as it is.
const fs = require('node:fs');
const path = require('node:path');
app.get('/pkg/app.wasm', async (req, res) => {
const bytes = await fs.promises.readFile(path.join(__dirname, 'pkg', 'app.wasm'));
res.type('application/wasm');
res.send(bytes);
});3Verify
The header now reads application/wasm.
curl -sI http://localhost:3000/pkg/app.wasm | grep -i '^content-type'Confirmed by expressjs.com
Cause 3 of 4
The Content-Type carries extra parameters
1Check
The WebAssembly Web API specification compares the MIME type with application/wasm and allows no extra parameters. A header that carries any added parameter is rejected with the same TypeError, so look at the exact bytes of the header.
curl -sI http://localhost:3000/pkg/app.wasm | grep -i '^content-type' | cat -A2Fix
Set the header to the bare type for .wasm files. With express.static, setHeaders runs on each file it sends.
app.use(express.static('public', {
setHeaders(res, filePath) {
if (filePath.endsWith('.wasm')) {
res.setHeader('Content-Type', 'application/wasm');
}
},
}));3Verify
The header line ends right after application/wasm.
curl -sI http://localhost:3000/pkg/app.wasm | grep -i '^content-type' | cat -AConfirmed by webassembly.github.io
Cause 4 of 4
The response is not OK or comes from another origin
1Check
The same specification rejects the promise with a TypeError when the response does not have an ok status or is not CORS-same-origin. A 404 whose body is an HTML page also fails the MIME check, so it shows the same message.
curl -sI http://localhost:3000/pkg/app.wasm | head -n 12Fix
Make the URL return the file with status 200 from your own origin.
// same origin: serve the file from the app's own static folder
app.use('/pkg', express.static(path.join(__dirname, 'pkg')));3Verify
The first line of the response is a 200.
curl -sI http://localhost:3000/pkg/app.wasm | head -n 1Confirmed by webassembly.github.io
How do I stop it coming back?
- Fetch the .wasm URL with curl as part of your deploy check, and compare the status and Content-Type with what the page expects.
- Serve .wasm files from express.static where you can, and keep fallbacks away from URLs with a file extension.
- If you cannot change the header, use fetch, arrayBuffer and WebAssembly.instantiate, which work on raw bytes; the MIME rule is stated for the streaming methods.
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
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 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 fix