AWS Lambda: Cannot Import Module

please forgive me, I'm brand new to Lambda and Node.

I'm trying to repeat this bastard to order pizza using the 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; 

File structure:

  • orderDominos.js
  • node_modules / Domino

I archived the files, uploaded them to Lambda and pointed the header to "index.handler"

What am I doing wrong?

Change: 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) 

+14
source share
7 answers

In my case, I referred to Handler as index.handler , but my root file name is app.js Changed this to index.js .

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

Must be:

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

Not

 zip file --> some_folder_name --> index.js package.json node_modules 
+20
source

For me it was a permissions problem, after I changed the permissions for the 'node_modules' folder to 777, archived and downloaded it, it worked.

+4
source

This problem also enters. For me, it decided that the file path is too long on a Windows machine. After zipping, I realized that the contents of node_modules is empty. I copied the files to go to a higher level, for example. C: \ User \ and pin the specified files. Hope this helps!

+1
source

I had the same problem and decided to solve it by following these steps.

  • Do not use the default zip parameter specified in finder on mac. use terminal for zip

cd folder name

zip -r foldername.zip *

  1. use export in all your js functions that you want to use in index.js file.

Say in javascript a.js file

 var func = function(){ } export.func = func ; 

In index.js

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

What worked for me was zip the following files and load the zip (after installing npm in the folder):

  • node_modules /
  • your_file1.js
  • your file2.js
  • your files.js
  • package.json
  • Package-lock.json
0
source

In our case, this is not a path or resolution problem. We got this error because we do npm prune --production before deployment, and we have some runtime packages that are incorrectly placed in devDependencies that are erased at this point. Unfortunately lambda only gives a vague error message.

0
source

x75c8.png

Install

 'npm install request' to the same folder of ----> index.js package.json node_modules 

zip This and upload to Lambda

-3
source

Source: https://habr.com/ru/post/1244615/


All Articles