AWS Lambda:无法导入模块

请原谅我,我在Lambda和Node上是全新的。

我正尝试复制此 git以使用AWS IoTbutton订购披萨。

我目前的代码是:

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但我的根文件名是index.handler 改变这个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,ziped和上传,它的工作。

也碰到这个问题。 为了解决这个问题,我意识到Windows机器上的文件path太长了。 在压缩之后,我意识到node_modules的内容是空的。 我将文件复制到更高级别的path,例如C:\ User \,然后压缩指定的文件。 希望这可以帮助!

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

  1. 不要在Mac中使用finder中提供的默认zip选项。 使用terminal来压缩

cd文件夹名称

zip -r foldername.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 } 

对我来说是什么工作是压缩以下文件,并上传压缩(在文件夹中进行npm安装后):

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