添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

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

if (typeof process.send === 'function') { 
    process.send('what I want to send');
                If you mean something different than the question states, then please edit your question to correct it.
– jfriend00
                Jun 2, 2015 at 0:50

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 () {};
                this is super lame IMO. all processes should have a parent - process.send should do nothing if nobody is listening, just like every other pub/sub type API. but it's ok, we can just litter the code with conditionals, as I mention in the edit to the OP
– Alexander Mills
                Jun 2, 2015 at 0:52
                Yeah there is likely a reason.  For now you could just put process.send = process.send || function () {}; at the head of your 'root' file.
– Alex McMillan
                Jun 2, 2015 at 0:54
                thanks Mr. McMillan, you seem to get it. Your solution looks like no worse then any others. This is a nasty one. I hope there is a really good reason for this...doubtful.
– Alexander Mills
                Jun 2, 2015 at 0:59
                @AlexMills If you're "littering" your code with conditionals for this, something is probably wrong.  Parent/child processes typically share very little code in a well-designed system.
– Aaron Dufour
                Jun 2, 2015 at 4:07
                @AlexMills Sure. process.send uses a JSON-over-IPC-fd mechanism, which is completely specific to node. It doesn't make sense to use that to try to send messages to a non-node process. So why not default it to a no-op? Because making a meaningfully-named method a no-op is disastrously undiscoverable and confusing. And you can easily turn it into a no-op yourself as this answer makes clear, whereas the opposite isn't really true (how would you determine that it's a no-op when its only effect is in another process?).
– Aaron Dufour
                Jun 2, 2015 at 17:13

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: https://github.com/Zimtir/SENT-template/issues/114

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.