node.js AWS Lambda:无法导入模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39282841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AWS Lambda: Unable to import module
提问by Ben Richards
please forgive me, I am totally new at Lambda and Node.
请原谅我,我是 Lambda 和 Node.js 的新手。
I am trying to replicate thisgit to order a pizza using an AWS IoT button.
我正在尝试复制此git 以使用 AWS IoT 按钮订购披萨。
My current code is:
我目前的代码是:
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: '[email protected]'
}
);
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;
The file structure is:
文件结构为:
- orderDominos.js
- node_modules/dominos
- 订购Dominos.js
- 节点模块/多米诺骨牌
I zipped the files, uploaded to Lambda, and pointed the header to "index.handler"
我压缩文件,上传到 Lambda,并将标题指向“index.handler”
What am I doing wrong?
我究竟做错了什么?
Edit: The error
编辑:错误
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)
回答by Prasanth Jaya
In my case, i mentioned Handleras index.handlerbut my root filename is app.js. Changing this to index.jsworked.
就我而言,我提到了Handlerasindex.handler但我的根文件名是app.js. 将此更改为index.js有效。
Also make sure the zip file has your index.js, node_modules and package.jsondirectly.
还要确保 zip 文件index.js, node_modules and package.json直接包含您的 .zip 文件。
Should be:
应该:
zip file --> index.js
package.json
node_modules
Not
不是
zip file --> some_folder_name --> index.js
package.json
node_modules
回答by Benjamin Guttmann
It was a permission issue for me, after I changed the permissions for the 'node_modules' folder to 777, zipped and uploaded it, it worked.
这对我来说是一个权限问题,在我将“node_modules”文件夹的权限更改为 777、压缩并上传后,它起作用了。
回答by Gabriel Wamunyu
Ran into this problem as well. What solved it for me was realizing that the file path was too long on a Windows machine. After zipping, I realized that the contents of node_modules was empty. I copied the files for zipping to a higher level path e.g. C:\User\ and zipped the specified files. Hope this helps!
也遇到了这个问题。为我解决的是意识到 Windows 机器上的文件路径太长。压缩后,我发现 node_modules 的内容是空的。我将要压缩的文件复制到更高级别的路径,例如 C:\User\ 并压缩指定的文件。希望这可以帮助!
回答by Ranjith Ramesh
I had the same issue and got it solved by the following the below steps
我遇到了同样的问题,并通过以下步骤解决了它
- dont use the default zip option provided in the finder in mac. use terminal to zip
- 不要使用 mac 中 finder 中提供的默认 zip 选项。使用终端压缩
cd foldername
cd 文件夹名
zip -r foldername.zip *
zip -r 文件夹名.zip *
- use exports in all your js functions which you want to use in the index.js file.
- 在所有要在 index.js 文件中使用的 js 函数中使用导出。
Say in Javascript file a.js
在 Javascript 文件 a.js 中说
var func = function(){
}
export.func = func ;
In index.js
在 index.js 中
var a = require('a.js')
exports.handler(event, context, callback){
a.func
}
回答by mnng
What worked for me was to zip the following files and upload the zip(after doing npm install in the folder):
对我有用的是压缩以下文件并上传 zip(在文件夹中执行 npm install 之后):
- node_modules/
- your_file1.js
- your file2.js
- your files.js
- package.json
- package-lock.json
- 节点模块/
- your_file1.js
- 你的 file2.js
- 你的 files.js
- 包.json
- 包-lock.json
回答by LeOn - Han Li
In our case, it is neither path or permission issue. We got this error because we do npm prune --productionbefore we deploy, and we have some runtime packages that is incorrectly placed under devDependencieswhich get wiped out during that phase. Unfortunately the lambda only gives a vague error message.
在我们的例子中,这既不是路径问题,也不是权限问题。我们得到这个错误是因为我们npm prune --production在部署之前就做了,我们有一些运行时包被错误地放置在devDependencies这些包下,在那个阶段被清除了。不幸的是,lambda 只给出了一个模糊的错误信息。

