【问题标题】:Node.js - Childprocess throws an error but execution of the command in terminal works under OS XNode.js - Childprocess 抛出错误,但在终端中执行命令在 OS X 下工作
【发布时间】:2018-07-29 10:23:32
【问题描述】:

我正在为编辑器 Atom 开发一个包。该软件包包含一个仅具有 CLI 的 python 程序的 GUI。通过使用我的包,用户只需单击一个图标。这些功能在 Windows 下经过了很好的测试,并且运行良好。现在我正在为 OS X 测试所有内容,然后麻烦就开始了。

在测试过程中,我发现了一些通过检查操作系统和附加参数修复的错误。

我正在使用 child_process 和 execexecSync

console.log("cmd "+cmd);  //just for whether cmd is correct
child_process.exec(cmd, (error, stdout, stderr) => {
    if (error) {
        //error handling
        console.log("error.msg "+error.message;
    }else{
     console.log("stdout " +stdout); //just for testing
     console.log("stderr " +stderr); //just for testing
    }
}

我的问题是命令执行正确 - python 程序生成文件但进程抛出错误。 我还在终端中测试了命令,没有出现错误。在 Windows 下我没有问题。

现在关于错误对象的更多信息:

  1. error.message = 命令失败:python_command /Users/jDoe/Document/test/test.md
  2. error.code = 119
  3. error.killed = 假
  4. error.stack

    Error: 
        Command failed: python_command /Users/jDoe/Document/test/test.md
          at ChildProcess.exithandler (child_process.js:223:12)
          at emitTwo (events.js:106:13)
          at ChildProcess.emit (events.js:191:7)
          at maybeClose (internal/child_process.js:885:16)
          at Process.ChildProcess._handle.onexit   
        (internal/child_process.js:226:5)"
    

python 程序无法读取语言环境并返回值为 None 但我需要编码。

有什么想法吗?

【问题讨论】:

    标签: node.js macos packages atom-editor child-process


    【解决方案1】:

    我读取了语言环境并将它们传递给进程。

    var env = Object.create( process.env );
    env.LANG=`en_US.UTF-8`;  // just for testing 
    iExec(cmd, {env: env, cwd: path},
    (error, stdout, stderr)  => { 
      if(error){
       // do error handling
      }
    });
    

    现在它工作正常。

    【讨论】:

      猜你喜欢
      • 2017-06-10
      • 1970-01-01
      • 2017-11-07
      • 2023-03-27
      • 2018-08-15
      • 2011-11-24
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      相关资源
      最近更新 更多