Node: Same command works in exec but not in execSync

Created on 1 Dec 2016  路  11Comments  路  Source: nodejs/node

  • Version: v6.7.0
  • Platform: Windows 64-bit
  • Subsystem:


This script work perfectly

const execSync = require('child_process').execSync;
exec("_data\\bin\\trec_eval_Windows ./_data/QueryFile/qrels.txt ./_results/test_query.txt", (err,std) => {
    console.log( std )
})

but this one would gives an error

const execSync = require('child_process').execSync;
var child = execSync("_data\\bin\\trec_eval_Windows ./_data/QueryFile/qrels.txt ./_results/test_query.txt")
console.log(child.toString())
child_process.js:526
    throw err;
    ^

Error: Command failed: _data\bin\trec_eval_Windows ./_data/QueryFile/qrels.txt ./_results/test_query.txt

    at checkExecSyncError (child_process.js:483:13)
    at execSync (child_process.js:523:13)
    at Object.<anonymous> (....\testexe.js:8:13)
    at Module._compile (module.js:556:32)
    at Object.Module._extensions..js (module.js:565:10)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)
    at Function.Module._load (module.js:424:3)
    at Module.runMain (module.js:590:10)
    at run (bootstrap_node.js:394:7)

And the input command works in the command line

Thank you

child_process invalid windows

Most helpful comment

try something like this

try {
    execSync(...);
} catch (err) {
    err.stdout;
    err.stderr;
    err.pid;
    err.signal;
    err.status;
    // etc
}

All 11 comments

Are you sure the error is null?

Your "works perfectly" has a typo, and doesn't print the err argument, so its impossible to know if it has an error (async functions don't throw). Do you have a standalone example we can run?

Sorry for the delay.
@sam-github Sorry I copy-pasted the wrong line, that should be

const exec = require('child_process').exec;

This is an example of this problem with the same directory structure.
test.zip

@s0m3on3 I just checked and the async version did give error, but the exit code was 1, which is partially error. Is it a way to get around it in the sync version?

@eugene2528 child_process.execSync(command[, options])
it says

If the process times out, or has a non-zero exit code, this method will throw. The Error object will contain the entire result from child_process.spawnSync()

I did try that but the returned object don't have any result other then the error message.
I got around it by create .bat script and force it to exit with exit code 0 and it worked.
But I think it is relatively a messy get around method...

try something like this

try {
    execSync(...);
} catch (err) {
    err.stdout;
    err.stderr;
    err.pid;
    err.signal;
    err.status;
    // etc
}

@s0m3on3 it works! Thank you!

me too, @s0m3on3
in shell, cmd is ok

python '/Users/xiatian/safe/myhktools/lib/struts/../../py/struts-scan.py' 'http://192.168.10.115:8080/QIMS/login.jsp' | grep '鐩爣瀛樺湪'
{ Error: Command failed: python '/Users/xiatian/safe/myhktools/lib/struts/../../py/struts-scan.py' 'http://192.168.10.115:8080/QIMS/login.jsp' | grep '鐩爣瀛樺湪'
    at checkExecSyncError (child_process.js:596:11)
    at Object.execSync (child_process.js:633:13)
    at Object.doCheck (/Users/xiatian/safe/myhktools/lib/struts/pythonBc.js:12:28)
    at fnCheckUrl.fnT (/Users/xiatian/safe/myhktools/lib/core_new.js:475:42)
    at fnCheckUrl.emit (events.js:187:15)
    at /Users/xiatian/safe/myhktools/lib/core_new.js:1229:7
    at /Users/xiatian/safe/myhktools/lib/core_new.js:1199:4
    at FSReqWrap.oncomplete (fs.js:150:5)
  status: 1,
  signal: null,
  output: [ null, <Buffer >, <Buffer > ],
  pid: 80153,
  stdout: <Buffer >,
  stderr: <Buffer > }

@eugene-yang How do you solve it? I have the same problem like you.

@hktalent @mabaoqing I have the same problem....

Same problem here. This should be reopened.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cong88 picture cong88  路  3Comments

akdor1154 picture akdor1154  路  3Comments

vsemozhetbyt picture vsemozhetbyt  路  3Comments

ksushilmaurya picture ksushilmaurya  路  3Comments

filipesilvaa picture filipesilvaa  路  3Comments