部署云功能时index.js出错

时间:2017-09-13 12:26:00

标签: python node.js google-cloud-functions

我正在尝试使用Cloud Function事件触发器执行python脚本。我的index.js有以下代码:

var PythonShell = require('python-shell');

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

exports.processFile = function(event, callback) {
  console.log('Processing file: ' + event.data.name);
  callback();
};

module.exports = PythonShell;

和package.json有以下代码:

{ 
   "name": "python-shell", 
   "version": "0.4.0", 
   "description": "Run Python scripts from Node.js with simple (but efficient) inter-process communication through stdio", 
   "keywords": [ 
   "python" 
    ], 
   "scripts": { 
    "test": "my_script.py" 
   },
   "dependencies" : {}
}

但该功能尚未创建。它显示如下错误: ERROR:

Deployment failure:
Function load error: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'python-shell'
    at Function.Module._resolveFilename (module.js:469:15)
    at Function.Module._load (module.js:417:25)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/user_code/index.js:7:19)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)

请告知如何解决?

2 个答案:

答案 0 :(得分:0)

请先尝试此命令

npm install python-shell

答案 1 :(得分:0)

正如错误堆栈跟踪所示,您尚未在package.json依赖项中指定python-shell。请将它添加到你的package.json。

要添加所需的依赖项而不手动更新package.json文件,

  1. cd到包含本地package.json
  2. 的文件夹
  3. npm install python-shell --save
  4. 当您打开package.json时,依赖项字段应指定模块及其semantic version。这是一个例子。

    "dependencies": {
      "python-shell": "^0.4.0"
    }
    

    此后有一些方法可以更新package.json文件中的版本,但是,这不在本问题的范围内。

    完成此操作后,尝试部署该功能。