AWS Lambda Error: "Cannot find module '/var/task/index'"

57,015

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:

In this case we expect that all our code will be in <code>bundle.ls</code> file

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.

Share:
57,015
Anthony Krivonos
Author by

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, 2021

Comments

  • Anthony Krivonos
    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
    Anthony Krivonos over 7 years
    Unfortunately, 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
    Dane Macaulay about 7 years
    i 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
    Alok Rajasukumaran almost 7 years
    How about windows?
  • DrDirk
    DrDirk almost 7 years
    also make sure to name the function index.js
  • andrhamm
    andrhamm over 6 years
    Is there any way to do this from outside the directory?
  • Qaz
    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
    Pardeep Jain over 6 years
    in windows git bast throwing error "command not found", how to resolve this any idea ?
  • Pardeep Jain
    Pardeep Jain over 6 years
    in windows git bast throwing error "command not found", how to resolve this any idea ?
  • Ashutosh Jha
    Ashutosh Jha over 6 years
    This is command of Ubuntu , please google it for windows or how to create zip from command prompt
  • Exadra37
    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
    C Bauer about 5 years
    I also ran in to this; [Windows] it was simply that I was right clicking on the API part of my repo and using Send To > Compressed Folder. This creates a zip with structure api/etc, hence the exception!
  • kbuechl
    kbuechl about 5 years
    If 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 the npm install --production to save space!