本文介绍了无法要求('child_process')。spawn,控制台说spawn不是函数,Python-Shell节点包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用外部软件包:

I am attempting to use an external package:

npm install [python-shell][1]

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

Right now, I have just the basic js file with the example the package comes with:

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会显示:

When I start up the server, the console.log says:

Uncaught TypeError: spawn is not a function

在python-shell软件包的index.js中,正确要求生成():

Within the index.js of the python-shell package, spawn is required correctly (similar case):

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

随后,它在软件包中的使用方式如下:

And later, it is used in the package like so:

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

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

So I'm not sure why console says it is not a function.

推荐答案

我试图遇到同样的问题运行这样的代码

I ran into this same problem trying to run code like this

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)

但是,添加生成需要修复的请求

however, adding spawn to the require fixed it

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

OR

      var {spawn} = require('child_process')

这有效很好...

这篇关于无法要求('child_process')。spawn,控制台说spawn不是函数,Python-Shell节点包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 06:30