Maaz Zindani

Loading the index…

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

WebAssembly compile fails: Incorrect response MIME type, expected application/wasm

ErrorTypeError: 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.

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

Fix it fast

  1. 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'
  2. 2Match what you see, then jump to its fix

    1. 1The response body is an HTML page, not the binary fileThe URL is wrong and a fallback route answers with index.htmlFix
    2. 2The response type is application/octet-streamA route sends the file as a raw BufferFix
    3. 3The header is application/wasm followed by more text, such as a charsetThe Content-Type carries extra parametersFix
    4. 4The status is 404 or 500, or the .wasm file lives on a different hostThe response is not OK or comes from another originFix

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

Did this fix it?

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

Did this fix it?

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 -A

2Fix

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 -A

Confirmed by webassembly.github.io

Did this fix it?

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 1

2Fix

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 1

Confirmed by webassembly.github.io

Did this fix it?

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

Next step

Want this handled for good?

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