Skip to content

promisify child_process.exec always rejects #19494

@njtrettel

Description

@njtrettel
  • Version:
    v8.9.4
  • Platform:
    Ubuntu 16.04

I'm having trouble promisifying child_process.exec. When I run with error or without, the promise is rejected. I've followed the docs and wrote this:

const { promisify } = require('util');
const execCallback = require('child_process').exec;
const exec = promisify(execCallback);

const fooBar = () => {
  const dir = `engine/Users/${user}`;
  const fileOne = `${process.cwd()}/${dir}/myFileOne.txt`;
  const fileTwo = `${process.cwd()}/${dir}/myFileTwo.txt`;
  const command = `diff --unchanged-group-format="" ${fileOne} ${fileTwo}`;

  return run(command)
    .then((stdout, stderr) => {
      console.log('ran diff');
      return stdout;
    })
    .catch(error => console.log('got error running diff'));
};

fooBar goes through the catch block with this error:

{
  killed: false,
  code: 1,
  signal: null,
  cmd: /*the correct diff cmd*/,
  stdout: /*the correct diff stdout*/,
  stderr: ''
}

An actual error (or at least a 'no such file') gives me code: 2, stdout: '', stderr: no such file error string

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.promisesIssues and PRs related to ECMAScript promises.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions