2016-09-19 121 views
1

當我通過一個npm腳本產生一個子進程時,我失去了ipc通道。當通過npm腳本產生一條消息從子進程到父進程

我有這樣

if (process.send) { process.send('child') } 

孩子腳本,如果產卵它像這樣另一個腳本,父進程將成功接收並打印消息

var spawn = require('child_process').spawn 
spawn('node', ['child.js'], {stdio: ['inherit', 'inherit', 'inherit', 'ipc'}) 

spawn.on('message', function (msg) { console.log(msg) }) 

但是如果我定義node child.js爲我的package.json中的一個npm腳本,如

"scripts": { 
    "child": "node child.js" 
} 

an d代替spawn('npm', ['run', 'child'], {stdio: ['inherit', 'inherit', 'inherit', 'ipc'})

process.send在我的子腳本中是未定義的。我想這是因爲npm現在正在產生該進程,並且該進程不會繼承我的stdio設置。也許有一種解決方法。 npm腳本文檔沒有提到該主題。

回答

0

這是一個預期的行爲。 npm只是一個代理,它會產生node並完成執行,因此您從來沒有通過運行節點應用程序的渠道。這是沒有辦法的。