无法要求('child_process')。spawn,console说spawn不是一个函数,Python-Shell节点包

问题描述 投票:3回答:1

我试图使用外部包:

npm install [python-shell][1]

现在,我只有基本的js文件和包附带的示例:

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');
});

my_script.py等一起

当我启动服务器时,console.log说:

Uncaught TypeError: spawn is not a function

在python-shell包的index.js中,正确需要spawn(similar case):

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

后来,它在包中使用如下:

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

但是,spawn似乎确实是一个功能:

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] }

所以我不确定为什么控制台说这不是一个功能。

node.js child-process spawn
1个回答
3
投票

我遇到了同样的问题,尝试运行这样的代码

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)

但是,添加spawn到require修复它

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

要么

      var {spawn} = require('child_process')

这工作正常....

© www.soinside.com 2019 - 2024. All rights reserved.