AWS Lambda:无法导入模块



请原谅,我是Lambda and Node的新手。

我正在尝试复制这个git来使用AWS IoT按钮订购披萨。

我当前的代码是:

var pizzapi = require('dominos');
var myStore = new pizzapi.Store(
    {
        ID: 'Example'
    }
);
var myAddress = new pizzapi.Address(
        {
            Street: 'Example',
            City: 'Example',
            Region: 'Example',
            PostalCode: 'Example'
        }
    );
var myCustomer = new pizzapi.Customer(
    {
        firstName: 'Example',
        lastName: 'Example',
        address: myAddress,
        phone: 'Example',
        email: 'Example@gmail.com'
    }
);
var order = new pizzapi.Order(
    {
        customer: myCustomer,
        storeID: myStore.ID
    }
);
var cardNumber='Example';
var cardInfo = new order.PaymentObject();
cardInfo.Amount = order.Amounts.Customer;
cardInfo.Number = cardNumber;
cardInfo.CardType = order.validateCC(cardNumber);
cardInfo.Expiration = 'Example';
cardInfo.SecurityCode = 'Example';
cardInfo.PostalCode = 'Example';
order.Payments.push(cardInfo);
function orderDominos(event, context) {
  var clickType = event.clickType;
  switch(clickType.toLowerCase()) {
    case "single": {
      order.addItem(
          new pizzapi.Item(
              {
                  code: 'P_14SCREEN',
                  options: {},
                  quantity: 1
              }
          )
      );
      break;
    }
    case "double": {
        order.addItem(
          new pizzapi.Item(
              {
                  code: 'P_14SCREEN',
                  options: {},
                  quantity: 1
              }
          )
      );
      break;
    }
    case "long": {
        order.addItem(
          new pizzapi.Item(
              {
                  code: 'P_14SCREEN',
                  options: {},
                  quantity: 1
              }
          )
      );
      break;
    }
  }
  order.validate(
      function(result) {
          console.log("Order is Validated");
      }
  );
  order.price(
      function(result) {
            console.log("Order is Priced");
      }
  );
  order.place(
      function(result) {
          console.log("Price is", result.result.Order.Amounts, "nEstimated Wait Time",result.result.Order.EstimatedWaitMinutes, "minutes");
          console.log("Order placed!");
          context.succeed(event);
      }
  );
}
exports.handler = orderDominos;

文件结构为:

  • orderDominos.js
  • node_modules/多米诺骨牌

我压缩了文件,上传到Lambda,并将头文件指向"index.handler"

我做错了什么?

编辑:错误

Unable to import module 'orderDominos': Error
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (/var/task/node_modules/dominos/src/http-json.js:1:74)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)

在我的情况下,我提到Handler作为index.handler,但我的根文件名是app.js。将此更改为index.js有效。

还要确保zip文件中直接包含index.js, node_modules and package.json

应:

zip file --> index.js
             package.json
             node_modules

zip file --> some_folder_name --> index.js
                                  package.json
                                  node_modules

这对我来说是一个权限问题,在我将'node_modules'文件夹的权限更改为777后,压缩并上传它,它工作了。

也遇到了这个问题。对我来说,解决这个问题的方法是意识到文件路径在Windows机器上太长了。压缩后,我意识到node_modules的内容是空的。我将要压缩的文件复制到更高级别的路径,例如C:User并压缩指定的文件。希望这对你有帮助!

在我们的例子中,这既不是路径问题也不是权限问题。我们得到这个错误是因为我们在部署之前做了npm prune --production,我们有一些运行时包被错误地放在devDependencies下,在这个阶段被清除了。不幸的是,lambda只给出一个模糊的错误消息。

我也有同样的问题,并通过以下步骤解决了

  1. 不要使用mac查找器中提供的默认zip选项。使用终端压缩

cd foldername

zip -r文件夹名。zip *

  • 在你想在index.js文件中使用的所有js函数中使用exports。
  • 在Javascript文件a.js中写入

    var func = function(){
    }
    export.func = func ; 
    

    In index.js

    var a = require('a.js')
    exports.handler(event, context, callback){
    a.func
    }
    

    对我来说有效的是压缩以下文件并上传压缩包(在文件夹中执行npm install后):

    • node_modules/
    • your_file1.js
    • 你file2.js
    • 你files.js
    • package.json
    • package-lock.json

    最新更新