2016-06-23 12 views
3

Ich bin versucht, ein externes Paket zu verwenden: npm python-shellKönnen erfordern (‚child_process‘) Laich, Konsole sagt Laich nicht eine Funktion ist, Phython-Shell Knoten Paket

Gerade jetzt installieren, habe ich nur die grundlegende js-Datei mit dem Beispiel kommt das Paket mit:

console.log('hey in main.js') 
var PythonShell = require('python-shell'); 

PythonShell.run('./my_script.py', function (err) { 
    if (err) throw err; 
    console.log('finished running python script'); 
}); 

Zusammen mit my_script.py usw

Wenn ich den Server starten, die console.log sagt: Uncaught TypeError: spawn is not a function

Innerhalb des index.js des Python-Shell-Pakets wird Laich richtig (similar case) erforderlich:

var spawn = require('child_process').spawn; 

Und später wird es im Paket wie so verwendet:

this.childProcess = spawn(pythonPath, this.command, options); 

Allerdings ist spawn scheint eine Funktion zu sein:

master$>node 
> require('child_process') 
{ ChildProcess: 
    { [Function: ChildProcess] 
    super_: 
     { [Function: EventEmitter] 
     EventEmitter: [Circular], 
     usingDomains: true, 
     defaultMaxListeners: 10, 
     init: [Function], 
     listenerCount: [Function] } }, 
    fork: [Function], 
    _forkChild: [Function], 
    exec: [Function], 
    execFile: [Function], 
    spawn: [Function], 
    spawnSync: [Function: spawnSync], 
    execFileSync: [Function: execFileSync], 
    execSync: [Function: execSync] } 

Also ich bin nicht sicher, warum c Onsole sagt, es ist keine Funktion.

Antwort

0

Ich lief in das gleiche Problem versucht, Code wie dieser

var spawn = require('child_process') 
var child = spawn('pwd') 

TypeError: spawn is not a function 
at Object.<anonymous> (/home/sailor/advancedNode/child_cluster_exec/spawn.js:5:13) 

jedoch in Fehler würde zu laufen, und fügte hinzu Laich zu den festen erfordern es

var spawn = require('child_process').spawn 
var child = spawn('pwd') 

ODER

 var {spawn} = require('child_process') 

das funktioniert gut ....