ipc.md
This file
Path
docs/ipc.md
54 lines · 2.2 KiB · Markdown
Last changed by
Move API reference to its own page (#996)ehmickybefore adoption7cde496
History of this fileTake it
ipc.md
📞 Inter-process communication
Exchanging messages
When the ipc option is true, the current process and subprocess can exchange messages. This only works if the subprocess is a Node.js file.
The ipc option defaults to true when using execaNode() or the node option.
The current process sends messages with subprocess.send(message) and receives them with subprocess.on('message', (message) => {}). The subprocess sends messages with process.send(message) and process.on('message', (message) => {}).
More info on sending and receiving messages.
// parent.js
import {execaNode} from 'execa';
const subprocess = execaNode`child.js`;
subprocess.on('message', messageFromChild => {
/* ... */
});
subprocess.send('Hello from parent');
// child.js
import process from 'node:process';
process.on('message', messageFromParent => {
/* ... */
});
process.send('Hello from child');
Message type
By default, messages are serialized using structuredClone(). This supports most types including objects, arrays, Error, Date, RegExp, Map, Set, bigint, Uint8Array, and circular references. This throws when passing functions, symbols or promises (including inside an object or array).
To limit messages to JSON instead, the serialization option can be set to 'json'.
const subprocess = execaNode({serialization: 'json'})`child.js`;
Next: 🐛 Debugging
Previous: ⏳️ Streams
Top: Table of contents