Since001
Since001

Reputation: 81

execSync in node.js doesn't run shell command properly

I'm trying to run a child_process.execSync shell command in node.js like this:

function test() {
    return new Promise(function(resolve, reject) {
        var execSync = require('child_process').execSync;
        var result = execSync('some executable I manually installed', { encoding: 'utf8', silent: true });

        if (result.search('something') > -1) {
            resolve("ok");
        }
        else if (result.search('something else') {
            resolve("nok");
        }
        else {
            reject(Error("ERROR"));
        }
    });
} 

test().then(function(result) {
  console.log(result);
}, function(err) {
  console.log(err);
});

However, I get the following error as an output.

{ [Error: Command failed: my shell command]
  error: null,
  cmd: 'my shell command',
  file: '/bin/sh',
  args: [ '/bin/sh', '-c', 'my chell command' ],
  options: 
   { silent: true,
     encoding: 'utf8',
     file: '/bin/sh',
     args: [ '/bin/sh', '-c', 'my shell command' ],
     envPairs: 
      [ 'TERM_PROGRAM=Apple_Terminal',
        'SHELL=/bin/bash',
        'TERM=xterm-256color',
        'TMPDIR=/var/folders/k5/qklhpvtj227_5vdnn2d_x6zr0000gn/T/',
        'Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.2zYqrvejoj/Render',
        'TERM_PROGRAM_VERSION=361.1',
        'OLDPWD=/usr/local/bin',
        'TERM_SESSION_ID=8F8D0040-84DE-41F4-84E6-A9481A09CE73',
        'USER=and',
        'SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.L5VoA4PiFl/Listeners',
        '__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0',
        'PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin',
        'PWD=/Users/and/Documents/',
        'XPC_FLAGS=0x0',
        'XPC_SERVICE_NAME=0',
        'SHLVL=1',
        'HOME=/Users/and',
        'LOGNAME=and',
        'LC_CTYPE=UTF-8',
        'DISPLAY=/private/tmp/com.apple.launchd.rwoZybokZk/org.macosforge.xquartz:0',
        '_=/usr/local/bin/node' ],
     stdio: [ [Object], [Object], [Object] ] },
  envPairs: 
   [ 'TERM_PROGRAM=Apple_Terminal',
     'SHELL=/bin/bash',
     'TERM=xterm-256color',
     'TMPDIR=/var/folders/k5/qklhpvtj227_5vdnn2d_x6zr0000gn/T/',
     'Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.2zYqrvejoj/Render',
     'TERM_PROGRAM_VERSION=361.1',
     'OLDPWD=/usr/local/bin',
     'TERM_SESSION_ID=8F8D0040-84DE-41F4-84E6-A9481A09CE73',
     'USER=and',
     'SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.L5VoA4PiFl/Listeners',
     '__CF_USER_TEXT_ENCODING=0x1F5:0x0:0x0',
     'PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin',
     'PWD=/Users/and/Documents/',
     'XPC_FLAGS=0x0',
     'XPC_SERVICE_NAME=0',
     'SHLVL=1',
     'HOME=/Users/and',
     'LOGNAME=and',
     'LC_CTYPE=UTF-8',
     'DISPLAY=/private/tmp/com.apple.launchd.rwoZybokZk/org.macosforge.xquartz:0',
     '_=/usr/local/bin/node' ],
  stderr: '',
  stdout: '',
  pid: 16931,
  output: [ null, '', '' ],
  signal: null,
  status: 10 }

I think it has something to do with /bin/sh (I'm OS X by the way), but I don't know how to fix this. I can run the shell command in the Terminal just fine, it just won't work in node with execSync. Also, when I try to run execSync with a command that comes with OS X (like ls), it runs perfectly fine...

Can anybody help? Thanks!

Upvotes: 1

Views: 5154

Answers (1)

Mohamed Elbahja
Mohamed Elbahja

Reputation: 639

if the last exec program return error code for example on this C code

#include <stdio.h>

int main(void)
{

    printf("hi");

    return 1;
}

the point is if the program not return 0 the node child_process throw error

My Solution:

const proc = require('child_process');

// just add " || true" after your command 
var output = proc.execSync('command || true');

it's work fine for me :)

Upvotes: 1

Related Questions