Skip to content

Latest commit

 

History

History
54 lines (38 loc) · 2.24 KB

ipc.md

File metadata and controls

54 lines (38 loc) · 2.24 KB
execa logo

📞 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