无法 require('child_process').spawn,控制台说 spawn 不是函数,Python-Shell 节点包

imy*_*mmy 4 spawn child-process node.js

我正在尝试使用外部包:

npm install [python-shell][1]
Run Code Online (Sandbox Code Playgroud)

现在,我只有基本的 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');
});
Run Code Online (Sandbox Code Playgroud)

随着my_script.py

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

Uncaught TypeError: spawn is not a function
Run Code Online (Sandbox Code Playgroud)

在 python-shell 包的 index.js 中,正确需要 spawn(类似情况):

var spawn = require('child_process').spawn;
Run Code Online (Sandbox Code Playgroud)

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

this.childProcess = spawn(pythonPath, this.command, options);
Run Code Online (Sandbox Code Playgroud)

但是,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] }
Run Code Online (Sandbox Code Playgroud)

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

Bar*_*ard 5

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

var spawn = require('child_process')
var child = spawn('pwd')
Run Code Online (Sandbox Code Playgroud)

会导致错误

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

但是,将 spawn 添加到 require 修复了它

 var spawn = require('child_process').spawn
 var child = spawn('pwd')
Run Code Online (Sandbox Code Playgroud)

或者

      var {spawn} = require('child_process')
Run Code Online (Sandbox Code Playgroud)

这工作正常....

  • 似乎在 reactjs 应用程序中不起作用:`TypeError: spawn is not a function` (3认同)