The module '@algolia/cache-common' is missing and cannot be located

summary: code works locally but not in lambda.

My AWS lambda function runs perfectly when tested locally, utilizing Algolia within a service in the server. Despite installing @algolia/cache-common, any call to the lambda results in a crash due to the error below. Is there a solution to this issue?

    "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 Algolia implementation is structured as shown:

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

My lambda setup includes a lambda and 3 module layers that have worked with other libraries, except for Algolia specifically. The Algolia-related packages are present when inspecting the lambda's packages.

I attempted installing the required package (@algolia/cache-common) directly, without success. Furthermore, installing @types/algolia did not resolve the issue either.

Have I overlooked something crucial?

Answer №1

The issue has been resolved by rectifying my mistake of splitting to layers and inadvertently skipping the initial index in the loop.

Answer №2

When you "install" dependencies such as @algolia/cache-common, they are installed locally.


Your locally installed dependencies will not automatically be available on AWS Lambda. In order for them to work in Lambda, just like your application code, these dependencies need to be deployed.


This is why it may work on your local machine but not in Lambda.

You didn't mention how you deploy your code. Tools like AWS SAM or the serverless framework typically handle the deployment of both your application code and its dependencies.

It seems like you might be deploying manually. This means that you will likely need to also deploy your node_modules folder to AWS Lambda.

Your deployment ZIP archive should have a structure like this:

node_modules/
index.js

The node_modules folder will contain many sub-folders and files. For the purpose of simplicity, we'll stick with just one .js file for this explanation.

Answer №3

Understanding the structure of your service and how you are incorporating Algolia modules is crucial. Is the Algolia integration part of the core service lambda or separate layer? Exploring whether bundling all dependencies within the service resolves any issues could be beneficial.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with a stationary directional light tracking the movement of a rotating object and/or changes in the camera perspective

I've been facing a challenge in implementing a day-night cycle with a directional light in an Earth model using custom shaders. Everything seems to work fine with the night and day maps, as well as the light, as long as I don't manipulate the cam ...

Having trouble getting NextJS to work with jsmpeg/node-rtsp-stream for displaying an RTSP stream

Exploring: https://github.com/kyriesent/node-rtsp-stream and How to display IP camera feed from an RTSP url onto reactjs app page? I attempted to showcase the RTSP stream from a CCTV but encountered an error. ReferenceError: document is not defined at scri ...

JavaScript Plugins for Cordova

The more I delve into the inner workings of Cordova, the clearer it becomes to me. Yet, one area that continues to perplex me is the structure of JavaScript plugins. Typically, I write my JavaScript code as follows, adhering to what I believe is the stand ...

Transform collapsible color upon materialize click

Is there a way to change the color of the collapsible header only when clicked? I'm struggling with adding the color inside the class element while calling the "connect" function. Can this be achieved? <div class="collapsible-header" onclick="conn ...

MySQL conditions in game development

When a player transfers an item to their character, the item type is communicated to the server. In scenarios where a player equips an armband with the type "bracelet," I want it to attempt placing the item ID in the leftbracer column of the game_moblist ...

Enhance Bootstrap modals by automatically adjusting the background shadow mask when resizing or changing the content within the modal window

Incorporated within my bootstrap modal window is a form alongside a link that triggers the jQuery functionality of .slideToggle(). By interacting with this link, a concealed div expands. Consequently, the size of the modal popover becomes fluid. Upon click ...

I'm confused as to why my React application is showing a blank screen even though I successfully imported and used an icon from Material UI. It compiled without any errors

I encountered an issue with my code when I added the "Avatar" line. To fix the problem of material UI not displaying properly, I had to use react icons instead. If possible, I would like recommendations for alternative packages that include the Avatar co ...

Developing a jQuery Plugin to Generate an Interactive Dropdown Menu

I have a task to dynamically create a select list in which users can add options after the select list has been created. Check out my code snippet below: <script type="text/html" id="select_field"> <div class='row& ...

Keeping your gh-pages in sync with your master/main branch is the key to maintaining an up-to-date

When I created a gh-pages using React, I encountered an issue where changes in the main branches were not syncing with gh-pages. I discovered that I didn't have to push all the updates to gh-pages or even to the master/main branch to update my site. ...

What is the best way to load a database URL asynchronously and establish a database connection prior to the initialization of an Express

My express.js app is set up to run on AWS lambda, with the database URL stored and encrypted in Amazon KMS. To access the URL, decryption using the AWS KMS service is required. // imports import mongoose from 'mongoose'; import serverless from & ...

Trigger ng-change event for each dropdown selection made in AngularJS

Currently, I have a dropdown menu that allows users to select a report for generation. When a user picks a report from the dropdown, it generates and downloads the report for mobile viewing. By utilizing ng-change, the system only detects when a user wants ...

Determine the sum of all the values entered into the text fields

On my ASP.Net page, there is a screen where users can select between 1 and 5 text boxes to input an amount. Depending on certain criteria, a specific number of these edit boxes are displayed - no hiding involved. So if I choose to display 3 boxes, only 3 w ...

Ways to update the content of a NodeList

When I execute this code in the console: document.querySelectorAll("a.pointer[title='Average']") It fetches a collection of Averages, each with displayed text on the page: <a class="pointer" title="Average" onclick="showScoretab(this)"> ...

How can you utilize the Array submission syntax within HTML coding?

I currently have numerous input fields within a form, and some of them are structured like this: <input name="agents[]" type="file" /> Additionally, imagine there is a plus button next to this field as shown below: <img src="plus.jpg" id="some_ ...

The C# counterpart to the JavaScript "OR assignment" concept

Is there a comparable feature in C# to JavaScript's assignment syntax var x = y || z;? This operation does not result in true/false. If y is defined, it assigns that value to x, otherwise it assigns z to x, even if it is undefined. Keep in mind, in J ...

Deploying my MEAN stack web application on GitHub Pages

My project can be found here on GitHub. I have created a gh-pages branch to host it on GitHub Pages. The link provided for my hosted site is here. As someone new to web apps and the MEAN stack, I am wondering how I can access my web app now that it is hos ...

Attain worldwide reach

I'm currently facing a Scope issue that has been quite challenging to resolve. The saying goes, "a picture is worth a thousand words," and in this case, it couldn't be more true. Whenever the OK or REJ buttons trigger the reject() function, passi ...

Tips on including variables within quotation marks in JavaScript

I'm currently working on a JavaScript project using Pug. My goal is to use Express and MongoDB to develop a CRUD application that generates an edit button for each post, triggering a specific event when clicked. However, instead of the desired resul ...

I'm unsure if I have the correct version of Node.js. Can someone confirm this for me?

I've been struggling to update my Node.js version for quite some time now. Whenever I check using node -v, it shows me as running: v0.10.40. However, when I visit nodejs.org, it displays the latest version as v7.4.0. I have already tried cleaning the ...

Encountering issues while trying to install Java using NPM

Encountering errors when attempting to run the following command to install java dependencies for NPM. NPM install -g java In need of assistance to fix this error. C:\WINDOWS\system32>npm i -g java [email protected] install C:\D ...