错误:产生sh ENOENT Node.js

时间:2017-03-28 11:35:23

标签: node.js twitter raspberry-pi sh child-process

我试图通过使用node.js从特定推特账号中的推文在我的覆盆子Pi上触发PIFM。但是当我在命令行上运行twitpifm.js时:

node twitpifm.js

我收到以下错误:

events.js:141
      throw er; // Unhandled 'error' event
      ^

Error: spawn sh ENOENT
    at exports._errnoException (util.js:874:11)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:178:32)
    at onErrorNT (internal/child_process.js:344:16)
    at doNTCallback2 (node.js:439:9)
    at process._tickCallback (node.js:353:17)
    at Function.Module.runMain (module.js:469:11)
    at startup (node.js:134:18)
    at node.js:961:3

我在PI2上使用此版本的PIFM

https://github.com/asmello/fm_transmitter

我编写了一个脚本(test.sh),它将从没有参数的命令行启动PIFm

 #!/bin/bash
    /home/pi/PIFm/fm_transmitter/bin/Release/fm_transmitter music.wav 103.50

我知道我需要在node.js中使用child_process来激活这个shell脚本但是遇到了麻烦;

首先我用:

调用子进程
var spawn = require('child_process').spawn // child process.spawn 

然后我尝试使用以下内容触发shell:

function thisTweet(){

    spawn('sh', ['test.sh'], {  // shell script I want to run.
    cwd:' /home/pi/Desktop/Twitter', // current working directory where my twitpifm.js file is 
    env: Object.assign({}, process.env, { PATH: process.env.PATH + '/home/pi/PIFm/fm_transmitter' }) //this sets a path to where test.sh is
    })

任何建议都会很棒!

1 个答案:

答案 0 :(得分:0)

好的..我得到了一个似乎正在使用的解决方案。来自这里的execfile:

https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

此代码有效:

const execFile = require('child_process').execFile;
const child = execFile('sh', ['/home/pi/Desktop/twitter/test'], (error, stdout, stderr) => {
  if (error) {
    throw error;
  }
  console.log(stdout);
});
}  

这让我可以从node.js

执行shell脚本