2016-09-06 6 views
1

Ich versuche, einige gute Bibliotheken zum Ausführen von Shell-Skript-Befehle mit Nodejs (zB: rm, cp, mkdir, etc ...) zu finden. Es gibt keine neuen klaren Artikel über mögliche Pakete. Ich habe viel über exec-sync gelesen. Aber manche sagen, dass es veraltet ist. Wahrlich, ich bin verloren und konnte nichts finden. Ich habe versucht, exec-sync zu installieren, aber ich habe den folgenden Fehler erhalten:Ausführen von Shell-Skript-Befehle mit Nodejs

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'. 

Haben Sie Vorschläge?

+0

Try fs-Extra: https://www.npmjs.com/package/fs-extra – datafunk

Antwort

3

Sie könnten einfach Knoten Child Process Kernmodul verwenden, oder zumindest verwenden Sie es, um Ihr eigenes Modul zu bauen.

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:

Insbesondere die exec() Methode.

child_process.exec()

spawns a shell and runs a command within that shell, passing the stdout and stderr to a callback function when complete.

Hier das Beispiel aus der Dokumentation.

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}`); 
}); 
+0

Gibt es eine synchrone Version von exec? –

+0

'child_process.execSync (Befehl [, Optionen])';) – DavidDomain

Verwandte Themen