学习child_process的过程中,有一个小demo
/**
* @fileOverview
* @author iNahoo
* @since 2018/1/30.
*/
"use strict";
const {spawn,execSync} = require('child_process');
const {resolve} = require('path');
process.chdir(resolve(__dirname, '../'));
function main() {
// execSync 可以正确执行
console.log(execSync('cd ../ && pwd').toString());
// spawn 不能正确执行
const chp = spawn(`cd ../ && pwd`);
chp.stdout.on('data', data => {
console.log(data.toString());
});
chp.on('close', () => {
resolve();
});
}
main();
execSync执行正确打出log
spawn返回报错
/Users/user/Desktop/project/AST
events.js:182
throw er; // Unhandled 'error' event
^
Error: spawn cd ../ && pwd ENOENT
at exports._errnoException (util.js:1022:11)
at Process.ChildProcess._handle.onexit (internal/child_process.js:189:19)
at onErrorNT (internal/child_process.js:366:16)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9)
at Function.Module.runMain (module.js:607:11)
at startup (bootstrap_node.js:158:16)
at bootstrap_node.js:575:3
相关分类