2016-04-13 5 views
6

Ich benutze node.js v4.4.4 und ich muss eine .bat Datei von node.js ausführen. Kommandozeile mit folgendem Pfad (Fenster-Plattform)Wie führe ich eine .bat Datei von node.js aus, die einige Parameter übergibt?

Von dem Standort der js-Datei für meine Knoten App, die .bat runnable ist:

'../src/util/buildscripts/build.bat --profile ../profiles/app.profile.js' 

Aber wenn der Knoten mit Ich kann es nicht laufen, keine spezifischen Fehler werden ausgelöst.

Was mache ich hier falsch?


var ls = spawn('cmd.exe', ['../src/util/buildscripts', 'build.bat', '--profile ../profiles/app.profile.js']); 

    ls.stdout.on('data', function (data) { 
     console.log('stdout: ' + data); 
    }); 

    ls.stderr.on('data', function (data) { 
     console.log('stderr: ' + data); 
    }); 

    ls.on('exit', function (code) { 
     console.log('child process exited with code ' + code); 
    }); 
+0

Verwandte: https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows – GibboK

+0

Related : https://medium.com/@graeme_boy/how-to-optimize-cpu-intensive-work-in-node-js-cdc09099ed41#.zbzjytwzw – GibboK

Antwort

8

Das folgende Skript mein Problem gelöst, im Grunde hatte ich:

  • zur absoluten Converting Pfadverweis auf .bat-Datei.

  • Übergabe von Argumenten an .bat mit einem Array.

    var bat = require.resolve('../src/util/buildscripts/build.bat'); 
    var profile = require.resolve('../profiles/app.profile.js'); 
    var ls = spawn(bat, ['--profile', profile]); 
    
    ls.stdout.on('data', function (data) { 
        console.log('stdout: ' + data); 
    }); 
    
    ls.stderr.on('data', function (data) { 
        console.log('stderr: ' + data); 
    }); 
    
    ls.on('exit', function (code) { 
        console.log('child process exited with code ' + code); 
    }); 
    

Im Folgenden eine Liste von nützlichen relevanten Artikel:

https://nodejs.org/api/child_process.html#child_process_asynchronous_process_creation

http://www.informit.com/articles/article.aspx?p=2266928

9

Sie sollten in der Lage sein, einen Befehl wie folgt auszuführen:

var child_process = require('child_process'); 

child_process.exec('path_to_your_executables', function(error, stdout, stderr) { 
    console.log(stdout); 
}); 
+0

Darf ich Sie fragen ... warum verwenden Sie .exec statt spawn? – GibboK

+0

Die Verwendung von spawn mit Shell-Optionen ist identisch mit exec. Dokumentation [hier] (https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows) –

+0

Bist du sicher? Entsprechend dieser Dokumentation sind sie unterschiedlich, und ich brauche Spawn, wenn möglich, ohne Exec. Könnten Sie bitte eine Spawn-Lösung zu Ihrer Antwort hinzufügen? http://www.hacksparrow.com/difference-between-spawn-and-exec-of-node-js-child_process.html – GibboK

Verwandte Themen