node.js脚本中的child_process.spawn ENOENT问题

时间:2019-07-07 16:39:13

标签: javascript node.js spawn

我正在尝试在节点脚本中运行以下命令:

/bin/sh -c '/Users/edwardthompson/IdeaProjects/evidentia4/evidentia4/package/macosx/e/wkhtmltopdf --page-size "letter" --footer-center "[page] of [topage]" --footer-left "Prepared Jul 7, 2019 by Edward A Thompson" --footer-right "Evidentia(c) 2012-2017" --footer-font-size "10" --footer-font-name "Times" --print-media-type /var/folders/45/2rx5vf4j47jf4hx5157q4q800000gn/T/ResearchSummaryReport-201967122421963.html /var/folders/45/2rx5vf4j47jf4hx5157q4q800000gn/T/ResearchSummaryReport-201967122421964.pdf'

从命令行运行,可以正常工作,但是我必须将所有引号括起来传递给/ bin / sh -c

当我在节点脚本中执行相同的操作时:

child = child_process.spawn('/bin/sh -c', ["'" + args.join(' ') + "'"], {stdio: 'inherit'});

我得到:

Message:
Uncaught exception: Error: spawn /bin/sh -c ENOENT
Stack:
error properties: Object({ errno: 'ENOENT', code: 'ENOENT', syscall: 'spawn /bin/sh -c', path: '/bin/sh -c', spawnargs: [ ''/Users/edwardthompson/IdeaProjects/evidentia4/evidentia4/package/macosx/e/wkhtmltopdf --page-size "letter" --footer-center "[page] of [topage]" --footer-left "Prepared Jul 7, 2019 by Edward A Thompson" --footer-right "Evidentia(c) 2012-2017" --footer-font-size "10" --footer-font-name "Times" --print-media-type /var/folders/45/2rx5vf4j47jf4hx5157q4q800000gn/T/ResearchSummaryReport-20196712254722.html /var/folders/45/2rx5vf4j47jf4hx5157q4q800000gn/T/ResearchSummaryReport-20196712254722.pdf'' ] })
Error: spawn /bin/sh -c ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:264:19)
    at onErrorNT (internal/child_process.js:456:16)
    at processTicksAndRejections (internal/process/task_queues.js:74:11)

想不通这个...

1 个答案:

答案 0 :(得分:1)

child_process.spawn正在寻找路径“ / bin / sh -c”的文件,该文件不存在。您应该改为将-c作为参数传递。

尝试一下:

child = child_process.spawn('/bin/sh', ["-c","'" + args.join(' ') + "'"], {stdio: 'inherit'});