MessagePort in message data must also be in transfer list
Production Risk
Low — caught immediately; always pair MessagePort usage with the transfer list.
Thrown when a MessagePort object is included in the message data of postMessage() but is not also listed in the transfer list. MessagePort objects must be transferred (not cloned) and must appear in the transferList array to be moved to the receiving context.
- 1Passing a MessagePort in the message without including it in the transferList
- 2Forgetting to add the port to the second argument of postMessage()
Triggered when postMessage() serialises the message and finds a MessagePort not in the transfer list.
const { Worker, MessageChannel } = require('worker_threads');
const { port1, port2 } = new MessageChannel();
const worker = new Worker('./w.js');
// port2 in message but not in transfer list — throws
worker.postMessage({ port: port2 }, []);expected output
Error [ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST]: MessagePort was found in message but not listed in transferList
Fix
Include the MessagePort in the transferList
WHEN When passing MessagePorts via postMessage()
const { Worker, MessageChannel } = require('worker_threads');
const { port1, port2 } = new MessageChannel();
const worker = new Worker('./w.js');
worker.postMessage({ port: port2 }, [port2]); // port2 in both message and transferListWhy this works
Listing the port in the transferList transfers ownership to the worker, satisfying the serialisation requirement.
const { Worker, MessageChannel } = require('worker_threads');
const { port1, port2 } = new MessageChannel();
const worker = new Worker('./w.js');
// port2 in message but not in transfer list — throws
worker.postMessage({ port: port2 }, []); // this triggers ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LISTtry {
// operation that may throw ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST
riskyOperation()
} catch (err) {
if (err.code === 'ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST') {
console.error('ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST:', err.message)
} else {
throw err
}
}// Validate inputs before calling the operation
function safe_err_missing_message_port_in_transfer_list(...args) {
// validate args here
return performOperation(...args)
}✕ Include MessagePort in message data without adding it to transferList
MessagePorts cannot be cloned; they must be explicitly transferred.
Node.js Error Codes Documentation
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev