AWS Lambda Error: "Cannot find module '/var/task/index'"
Solution 1
Fixed it! My issue was that I tried to zip the file using my Mac's built-in compression function in Finder.
If you're a Mac user, like me, you should run the following script in terminal when you are in the root directory of your project (folder containing your index.js
, node_modules
, etc. files).
zip -r ../yourfilename.zip *
For Windows:
Compress-Archive -LiteralPath node_modules, index.js -DestinationPath yourfilename.zip
Solution 2
Update to the accepted answer: When this error occurs, it means your zip file is not in the valid form which AWS requires.
If you double click on zip you will find your folder inside that your code file,but lambda wants that when you double click on zip it shoud show direct code files.
To achieve this:
open terminal
cd your-lambda-folder
zip -r index.zip *
Then, upload index.zip
to AWS Lambda.
Solution 3
Check that file name and handler name are same:
That means that zip
file has bundle.js
file that exports handler
function:
exports.handler = (event, context, callback) => {//...}
Solution 4
In my case it was because I had the handler file in inner src directory.
I had to change the 'Handler' property within Lambda from:
index.handler
to
src/index.handler
Solution 5
This is probably a permissions issue with files inside your deployment zip.
Try chmod 777
your files before packaging them in a zip file.
Anthony Krivonos
I'm a second-year student enrolled in the Fordham University-Columbia University dual degree engineering program for computer science. Since joining the Software Engineering Major during my junior year of high school, I've participated in mobile app development projects and engineering internships geared toward building interactive user interfaces and web servers. Recently, I have completed a full stack web developer internship for n-Tier Financial Solutions, a fintech startup based in New York City. There, I developed the front- and back-ends of Control Workbench, a data aggregation program used by some of the largest banks and financial institutions in the world. On my own, I have been strengthening my skills in JavaScript interactive media programming using Angular 4 and Ionic 3, and am super excited to unveil a startup a peer and I have been working on for several months. As an obsessive web and app developer, I'm always eager to work with other motivated programmers on great ideas that change the way people interact with technology!
Updated on June 15, 2021Comments
-
Anthony Krivonos almost 3 years
Node.js Alexa Task Issue
I'm currently coding a Node.js Alexa Task via AWS Lambda, and I have been trying to code a function that receives information from the OpenWeather API and parses it into a variable called
weather
. The relevant code is as follows:var request = require('request'); var weather = ""; function isBadWeather(location) { var endpoint = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&APPID=205283d9c9211b776d3580d5de5d6338"; var body = ""; request(endpoint, function (error, response, body) { if (!error && response.statusCode == 200) { body = JSON.parse(body); weather = body.weather[0].id; } }); } function testWeather() { setTimeout(function() { if (weather >= 200 && weather < 800) weather = true; else weather = false; console.log(weather); generateResponse(buildSpeechletResponse(weather, true), {}); }, 500); }
I ran this snippet countless times in Cloud9 and other IDEs, and it seems to be working flawlessly. However, when I zip it into a package and upload it to AWS Lambda, I get the following error:
{ "errorMessage": "Cannot find module '/var/task/index'", "errorType": "Error", "stackTrace": [ "Function.Module._load (module.js:276:25)", "Module.require (module.js:353:17)", "require (internal/module.js:12:17)" ] }
I installed module-js, request, and many other Node modules that should make this code run, but nothing seems to fix this issue. Here is my directory, just in case:
- planyr.zip - index.js - node_modules - package.json
Does anyone know what the issue could be?
-
Anthony Krivonos over 7 yearsUnfortunately, that doesn't seem to fix the issue. I tried compressing the
planyr
folder within another folder to no avail. My handler name and main JavaScript file names match (index
). -
Dane Macaulay about 7 yearsi was zipping like so: "zip -r folder folder.zip" and of course this failed. thanks for the tip to just zip the files and not the directory
-
Alok Rajasukumaran almost 7 yearsHow about windows?
-
DrDirk almost 7 yearsalso make sure to name the function
index.js
-
andrhamm over 6 yearsIs there any way to do this from outside the directory?
-
Qaz over 6 years@andrhamm Yes. The format is
zip -r /path/to/destination.zip /path/to/source/directory/*
. That zips the contents of the directory. If you want to zip the directory itself too, use /path/to/source/directory without the *. -
Pardeep Jain over 6 yearsin windows git bast throwing error "command not found", how to resolve this any idea ?
-
Pardeep Jain over 6 yearsin windows git bast throwing error "command not found", how to resolve this any idea ?
-
Ashutosh Jha over 6 yearsThis is command of Ubuntu , please google it for windows or how to create zip from command prompt
-
Exadra37 about 5 years
chmod 777
is a bad advice... this is opening permissions to public to write, read and execute. Please always find the route cause of the issue. Always try to understand the security implications of executing whatsoever someone in the internet tell you to do. Security is important and must be in our code by default. -
C Bauer about 5 yearsI also ran in to this; [Windows] it was simply that I was right clicking on the
API
part of my repo and usingSend To > Compressed Folder
. This creates a zip with structureapi/etc
, hence the exception! -
kbuechl about 5 yearsIf you have an issue with your node modules after uploading the zip you created using the zip command here, check to see that this issue didn't happen to you. Looks like
Compress-Archive
can cause the node modules to be uploaded as a flat tree. Also if you have a number of dev dependencies, be sure to delete your node modules folder first and use thenpm install --production
to save space!