简体   繁体   English

错误:生成sh ENOENT Node.js

[英]Error: spawn sh ENOENT Node.js

I am trying to trigger PIFM on my raspberry Pi by tweets from a specific twitter account using node.js. 我正在尝试使用node.js通过特定Twitter帐户的推文在树莓派上触发PIFM。 However when I run twitpifm.js on the command line: 但是,当我在命令行上运行twitpifm.js时:

node twitpifm.js

I get the following error: 我收到以下错误:

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

I am using this version of PIFM on my PI2 我在我的PI2上使用的是PIFM版本

https://github.com/asmello/fm_transmitter https://github.com/asmello/fm_transmitter

I have written a script (test.sh) that will start PIFm from command line without parameters 我已经编写了一个脚本(test.sh),该脚本将从命令行启动PIFm,而无需参数

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

I am aware that I need to need to use a child_process within my node.js to activate this shell script but am having trouble with it; 我知道我需要在node.js中使用child_process来激活该shell脚本,但是遇到了麻烦。

First I invoke child process with: 首先,我通过以下方式调用子进程:

var spawn = require('child_process').spawn // child process.spawn 

I then try to trigger the shell with the following: 然后,我尝试使用以下命令触发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
    })

Any suggestions would be great! 任何建议都很好!

Ok..I got a solution that seems to be working using . 好的..我有一个似乎正在使用的解决方案。 execfile from here: execfile从这里:

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

This code worked: 此代码有效:

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);
});
}  

This lets me execute a shell script from node.js 这使我可以从node.js执行shell脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM