16

please forgive me, I am totally new at Lambda and Node.

I am trying to replicate this git to order a pizza using an AWS IoT button.

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: '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;

The file structure is:

  • orderDominos.js
  • node_modules/dominos

I zipped the files, uploaded to Lambda, and pointed the header to "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)
Ben Richards
  • 171
  • 1
  • 1
  • 4

6 Answers6

23

In my case, i mentioned Handler as index.handler but my root filename is app.js. Changing this to index.js worked.

Also make sure the zip file has your index.js, node_modules and package.json directly.

Should be:

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

Not

zip file --> some_folder_name --> index.js
                                  package.json
                                  node_modules
Prasanth Jaya
  • 4,407
  • 2
  • 23
  • 33
4

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.

coffee-grinder
  • 26,940
  • 19
  • 56
  • 82
  • Thanks a lot. I was banging my head for past 1 hr Everything was fine i was zipping the folder correctly before uploading ... there were files in node_modules . Still i was getting the error, then i saw ur answer Thanks :) – vikneshwar Dec 08 '18 at 20:50
1

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!

Gabriel Wamunyu
  • 734
  • 9
  • 25
1

In our case, it is neither path or permission issue. We got this error because we do npm prune --production before we deploy, and we have some runtime packages that is incorrectly placed under devDependencies which get wiped out during that phase. Unfortunately the lambda only gives a vague error message.

LeOn - Han Li
  • 9,388
  • 1
  • 65
  • 59
0

I had the same issue and got it solved by the following the below steps

  1. dont use the default zip option provided in the finder in mac. use terminal to zip

cd foldername

zip -r foldername.zip *

  1. use exports in all your js functions which you want to use in the index.js file.

Say in Javascript file a.js

var func = function(){

}

export.func = func ; 

In index.js

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

a.func

}
0

What worked for me was to zip the following files and upload the zip(after doing npm install in the folder):

  • node_modules/
  • your_file1.js
  • your file2.js
  • your files.js
  • package.json
  • package-lock.json
mnng
  • 1