AWS Lambda:无法导入模块

16

请原谅我,我对Lambda和Node完全不熟悉。

我正在尝试复制代码来使用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/dominos

我将文件压缩,上传到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)


1
请不要发布这样的问题,而不包括实际的错误信息。 - Mark B
抱歉,我刚刚进行了编辑。 - Ben Richards
6个回答

23

在我的情况下,我将 Handler 命名为 index.handler,但我的根文件名是 app.js。将其更改为 index.js 后可以正常工作。

同时确保压缩文件包含了你的 index.js,node_modules 和 package.json 文件。

应该是:

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

不是

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

对于大多数用户来说,如果他们直接在Windows或Linux上压缩项目文件夹,则+1是通用解决方案。 - Abdul Hameed
这是大家都会停下来的地方。 - Arun Yokesh

4

对我来说,这是一个权限问题。在将“node_modules”文件夹的权限更改为777、压缩并上传后,它就可以正常工作了。


非常感谢。我已经苦思冥想了一个小时,一切都很好,我在上传之前正确地压缩了文件夹……其中有node_modules中的文件。但我仍然遇到错误,然后我看到了你的答案。谢谢 :) - vikneshwar

1
我也遇到了这个问题。解决方法是意识到在Windows机器上的文件路径太长了。压缩后,我发现node_modules的内容为空。我将要压缩的文件复制到更高级别的路径(例如C:\用户\),然后压缩指定的文件。希望这能有所帮助!

2
哦,我以为你用Linux修好了xD。 - Purefan

1
在我们的情况下,这不是路径或权限问题。我们之所以出现此错误,是因为在部署之前执行了npm prune --production,并且我们有一些运行时包被错误地放置在devDependencies下,在该阶段被清除。不幸的是,Lambda只会给出模糊的错误消息。

0
我的解决方案是将以下文件压缩并上传zip文件(在文件夹中执行npm install后):
  • node_modules/
  • your_file1.js
  • your file2.js
  • your files.js
  • package.json
  • package-lock.json

0

我曾遇到相同的问题,并通过以下步骤解决了它

  1. 不要使用mac中finder提供的默认zip选项。请使用终端进行压缩

cd文件夹名称

zip -r文件夹名称.zip *

  1. 在您想在index.js文件中使用的所有js函数中使用exports。

例如Javascript文件a.js

var func = function(){

}

export.func = func ; 

在index.js文件中
var a = require('a.js')
exports.handler(event, context, callback){

a.func

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接