【问题标题】:AWS Lambda: Unable to import moduleAWS Lambda:无法导入模块
【发布时间】:2019-10-07 23:50:34
【问题描述】:

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

我正在尝试复制 this 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)

【问题讨论】:

  • 请不要在没有包含实际错误信息的情况下发布此类问题。
  • 抱歉刚刚在里面编辑了

标签: node.js amazon-web-services aws-lambda


【解决方案1】:

就我而言,我提到Handlerindex.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

【讨论】:

  • +1 对于大多数用户来说,如果他们直接在 windows 或 linux 上压缩项目文件夹,这是通用的解决方案。
  • 这是每个人都会停下来的地方
【解决方案2】:

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

【讨论】:

  • 非常感谢。过去 1 小时我一直在敲我的头一切都很好,我在上传之前正确地压缩了文件夹...... node_modules 中有文件。我仍然收到错误,然后我看到了你的回答谢谢:)
【解决方案3】:

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

【讨论】:

  • 哦,我以为你用 linux xD 修复了它
【解决方案4】:

我遇到了同样的问题,通过以下步骤解决了

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

cd 文件夹名称

zip -r 文件夹名称.zip *

  1. 在所有要在 index.js 文件中使用的 js 函数中使用导出。

在Javascript文件a.js中说

var func = function(){

}

export.func = func ; 

在 index.js 中

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

a.func

}

【讨论】:

    【解决方案5】:

    对我有用的是压缩以下文件并上传 zip(在文件夹中执行 npm install 之后):

    • node_modules/
    • your_file1.js
    • 你的 file2.js
    • 你的文件.js
    • package.json
    • package-lock.json

    【讨论】:

      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-18
        • 2019-01-05
        • 2020-10-05
        • 1970-01-01
        • 1970-01-01
        • 2020-03-30
        • 2019-11-01
        • 1970-01-01
        相关资源
        最近更新 更多