Search code examples
javascriptnpmaws-lambdanode-modulesalgolia

Cannot find module:'@algolia/cache-common'"


note (tl:dr): everything works locally but not in lambda.

I have a lambda function in AWS and when I run the server locally everything works perfectly, the usage of algolia is being made inside a service, which is in an endpoint inside my server. I tried installing @algolia/cache-common and it didn't help either. Every call that is made to the lambda crashes the entire app because of this error. is there any way to fix it? the error is the following:

    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module '@algolia/cache-common'
    Require stack:
    /opt/nodejs/node_modules/algoliasearch/dist/algoliasearch.cjs.js
    /opt/nodejs/node_modules/algoliasearch/index.js
    /var/task/dist/api/v1/services/algolia.service.js 
    /var/task/dist/api/v1/handlers/jobs.handler.js
    /var/task/dist/api/v1/controllers/jobs.controller.js
    /var/task/dist/api/v1/v1.routes.js
    /var/task/dist/api/routes.js
    /var/task/dist/serverless.js
    /var/runtime/UserFunction.js
    /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module '@algolia/cache-common'",
        "Require stack:",
        "- /opt/nodejs/node_modules/algoliasearch/dist/algoliasearch.cjs.js",
        "- /opt/nodejs/node_modules/algoliasearch/index.js",
        "- /var/task/dist/api/v1/services/algolia.service.js",
        "- /var/task/dist/api/v1/handlers/jobs.handler.js",
        "- /var/task/dist/api/v1/controllers/jobs.controller.js",
        "- /var/task/dist/api/v1/v1.routes.js",
        "- /var/task/dist/api/routes.js",
        "- /var/task/dist/serverless.js",
        "- /var/runtime/UserFunction.js",
        "- /var/runtime/index.js",
        "    at _loadUserApp (/var/runtime/UserFunction.js:202:13)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:242:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:1085:14)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
        "    at Module.load (internal/modules/cjs/loader.js:950:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
        "    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)",
        "    at internal/main/run_main_module.js:17:47"
    ] 

the way I use the algolia is like the following:

const applicationId: any = config.get("ALGOLIA.APPLICATION_ID");
const apiKey: any = config.get("ALGOLIA.ADMIN_API_KEY");
const client = algoliasearch(applicationId, apiKey);
const index = client.initIndex("my-actual-index"); 

my lambda structure is a lambda and 3 module layers, which worked for every library I used, but doesn't work for algolia in particular. when I watch the lambda's packages I can see algolia related packages

I tried installing the exact package(@algolia/cache-common) and it didn't do anything, also tried instally @types/algolia and didn't work as well.

is there anything I missed?


Solution

  • When you "install" dependencies like @algolia/cache-common you are installing them locally.


    Your installed dependencies are not automatically available on AWS Lambda. Like your application code, your dependencies need to be deployed as well.


    That's why it works on your local machine but not in Lambda.

    You did not write anything on how you deploy your code. Tools like AWS SAM or the serverless framework usually take care of not only deploying your application code but also it's dependencies.

    So I imagine you are deploying by hand. That means that you most likely will have to also deploy your node_modules folder to AWS Lambda.

    Your deployment ZIP archive should look like this:

    node_modules/
    index.js
    

    The node_modules folder will have a lot of sub-folders etc and obviously, you can have more than one .js file. But for the purpose of this post we leave it at that.