AWS Lambda Handler TypeError: callback must be a function if provided

AWS Lambda Handler TypeError: callback must be a function if provided

我正在关注link:How Can I write an AWS Lambda Script that Runs a Protractor / Selenium Browser Automation Script? 我在处理程序中实现了与答案中给出的代码相同的代码:

'use strict';
module.exports.runtest = (event, context, callback) => {

  var npm = require('npm');
  var path = require('path');
  var childProcess = require('child_process');
  var args = ['conf.js'];

  npm.load({}, function() {
    var child = childProcess
    .fork(path.join(npm.root, 'protractor/bin/protractor'), args)
    .on('close', function(errorCode) {
      const response = {
        statusCode: 200,
        body: JSON.stringify({
          message: `Selenium Test executed on BrowserStack!  Child process Error Code: ${errorCode}`,
        }),
      };
      callback(null, response);
    });
    process.on('SIGINT', child.kill);
  });
};

但是在调用 lambda 函数时,出现以下错误:

{
  "errorType": "TypeError",
  "errorMessage": "callback must be a function if provided",
  "trace": [
    "TypeError: callback must be a function if provided",
    "    at Object.load (/var/task/node_modules/npm/lib/npm.js:163:13)",
    "    at Runtime.module.exports.runtest [as handler] (/var/task/handler.js:10:7)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

这里有什么帮助吗?

npm.load函数只接受一个参数,它是一个回调函数。然而,你传入 {} 作为第一个参数,因此你会得到一个类型错误,指出传递的值不是函数。

事实上,您使用的是来自另一个有效页面的确切代码,这可能是由于 npm 的不同版本,特别是因为您所指的 post 已经很旧了。