Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

process.send is conditionally defined in Node.js

Writer Mia Lopez

For whatever reason, in Node.js, the function process.send is defined in some environments but not defined in others. For instance, when I fork a child process from a parent process in Node.js like so:

//parent process
var cp = require('child_process');
var k = cp.fork('./child.js',['arg1','arg2','arg3']);
k.send('fail'); //k.send is defined...
process.send("ok, let's try this..."); //process.send is NOT defined

inside the child process:

//child.js
process.send('message'); //process.send is defined, and will send a message to the parent process above

why is process.send conditionally defined in some Node.js processes but not others? Seems like a poor design decision by Node.js architects.

The only way I know how to get around this is:

if (typeof process.send === 'function') { process.send('what I want to send');
}
4

2 Answers

Child processes have a process.send method to communicate back with the process that spawned them, while the root process doesn't have any "parent" to communicate, so its not there. From the docs:

In the child the process object will have a send() method, and process will emit objects each time it receives a message on its channel.

To avoid having to "litter the code with conditionals", a temporary solution might be to just put a "noop" function in its place at the top of any "root" files you might be spawning processes from:

process.send = process.send || function () {};
8

Workaround for Sapper & Svelte usage:

Basically, this error can be received in flow when you provided SAPPER_EXPORT variable in your environment.

More details about issue:

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy