使用nodejs执行shell脚本命令
我想找到一些好的库来使用nodejs执行shell脚本命令(例如:rm,cp,mkdir等)。最近没有关于可能软件包的明确文章。我读了很多关于exec-sync的信息。但有人说它已被弃用。真的,我迷路了,找不到任何东西。我尝试安装exec-sync,但收到以下错误:使用nodejs执行shell脚本命令
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'.
您有什么建议吗?
您可以简单地使用节点Child Process核心模块,或者至少使用它来构建您自己的模块。
The child_process module provides the ability to spawn child processes in a manner that is similar, but not identical, to popen(3). This capability is primarily provided by the child_process.spawn() function:
特别是exec()
方法。
child_process.exec()
spawns a shell and runs a command within that shell, passing the stdout and stderr to a callback function when complete.
这里是来自文档的示例。
Spawns a shell then executes the command within that shell, buffering any generated output.
const exec = require('child_process').exec;
exec('cat *.js bad_file | wc -l', (error, stdout, stderr) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
是否有高管的同步版本? –
'child_process.execSync(command [,options])';) – DavidDomain
尝试FS-EXTRA:https://www.npmjs.com/package/fs-extra – datafunk