The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

BarryM (3) [Avatar] Offline
#1
Hi, I've been following along with the book, when I go to test my lambda on AWS I get the following:

Unable to import module 'index': 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/aws-sdk/lib/xml/builder.js:2:15)
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)

Any advice please?
BarryM (3) [Avatar] Offline
#2
I've been Googling this error all morning and can confirm that my zip file contains node_modules and index.js is in the root of the zip file.
Lambda configuration has index.handler as the handler.

Am I missing something really obvious (not familiar with node.js, usually work in Python)?
472618 (13) [Avatar] Offline
#3
check the permissions of your index.js file
try running
chmod 777 *
in the directory of the function before deploying

-------------------------
update:
I'm doing another run through the book for review, my above suggestion didnt work in cygwin (this time) but removing *.json from the package.json file, as outlined by others below, did work.


Thanks
BarryM (3) [Avatar] Offline
#4
I'm running on windows so chmod isn't available
Hani (2) [Avatar] Offline
#5
I had the same problem but was able to resolve it after reading a related post at the AWS Lambda forum - https://forums.aws.amazon.com/thread.jspa?threadID=181471

In brief, I removed the "*.json" from the predeploy script in package.json.

Change: "predeploy": "zip -r Lambda-Deployment.zip * -x *.zip *.json *.log"
to: "predeploy": "zip -r Lambda-Deployment.zip * -x *.zip *.log"

This might not be the best solution but it did get me past the "unable to import module "index": Error at Function.Module..." error.
425617 (8) [Avatar] Offline
#6
I've got the same problem with a code from the book "WS Lambda in Action", chapter 10 authentication service runs the same error on a Lambda function.

Yet when re-saving the code manually, problem disappears...

Any ideas?
Chib (1) [Avatar] Offline
#7
Hani wrote:I had the same problem but was able to resolve it after reading a related post at the AWS Lambda forum - https://forums.aws.amazon.com/thread.jspa?threadID=181471

In brief, I removed the "*.json" from the predeploy script in package.json.

Change: "predeploy": "zip -r Lambda-Deployment.zip * -x *.zip *.json *.log"
to: "predeploy": "zip -r Lambda-Deployment.zip * -x *.zip *.log"

This might not be the best solution but it did get me past the "unable to import module "index": Error at Function.Module..." error.


Thanks Hani, that also resolved my issue. So is this an error in the book? The only logical explanation for this is that the .json files within node_modules are required to identify various modules entry files, without the package.json the main field must default to index.js which may not exist.
MacFlecknoe (22) [Avatar] Offline
#8
Thanks for the help! I was stuck on this for hours.