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

What is the best way to export a React Material-UI component with two separate styles objects in Material-UI V1?

We are currently utilizing material-ui version 1 and composing Higher Order Components (HOC) with the recompose utility. Our issue arises from having two separate styles objects - one defined within the component itself, and another general style object th ...

Presenting a trio of distinct tables each accompanied by its own unique button option

I am attempting to create a functionality where there are 3 buttons and when a user clicks on one of them, it shows the corresponding table while hiding the other two. I have experimented with using getElementById to manipulate the display property of the ...

A step-by-step guide on effectively swapping out every element in an array with its corresponding index location

After pondering over this question and conducting a search to see if it has been asked before, I couldn't quite find the answer due to difficulty in phrasing my inquiry. In case this question has already been addressed, I apologize for any duplication ...

Step-by-step guide on installing both Angular and Nodejs within a single folder

I'm diving into the world of NodeJs and Angular, and I recently created a simple NodeJS application following instructions from this link. However, I encountered an issue when trying to install Angular alongside NodeJS. After running ng new angular-cr ...

Creating a new row does not result in the creation of a new span displaying the character count message

Every description field should have its own character counter, with different SpanIDs displayed in respective SpanIds for each new row. How can this be achieved? <div class="row"> <div class="col-s ...

Issue with HighCharts: Bar columns not extending to the x-Axis when drilling up

I am encountering an issue with HighChart/HighStock that I need help with. To illustrate my problem, I have set up a JSFiddle. The problem arises when a user drills down on a bar column, causing the y-axis to shrink and consequently making the x-axis appea ...

What is preventing jQuery 3 from recognizing the '#' symbol in an attribute selector?

After updating my application to jQuery 3, I encountered an issue during testing. Everything seemed to be working fine until I reached a section of my code that used a '#' symbol in a selector. The jQuery snippet causing the problem looks like th ...

nuxt-link: take me to the identical position with the hash in the URL

I'm facing an issue with the <nuxt-link> component in my Nuxt application: The first time I click on the link, everything works perfectly and the page is changed as expected. However, if I scroll down a bit and try clicking the link again, noth ...

What steps can be taken to modify this jQuery code so that any changes made are also saved to localStorage

I have successfully used jQuery to change the background color of all my divs with the same class (userNumber1) when I check its checkbox. However, I am now looking for a way to save these changes to localStorage, so that they persist each time the page is ...

Discovering common elements in various arrays of objects

Details: record1 = [{"site": "The Blue Tiger", "zipcode": "E1 6QE"}, {"site": "Cafe Deluxe", "zipcode": "E6 5FD"}] record2 = [{"site": "Blue Tiger", "zi ...

I'm struggling with an issue of being undefined in my React.js project

This code snippet is from my app.js file import React, { useState, useEffect } from "react"; import { v4 as uuid } from "uuid"; import "./App.css"; import Header from "./Header"; import AddContact from "./AddCo ...

What is the optimal version of NPM to utilize?

Upon visiting https://www.npmjs.com/package/npm, the latest release displayed is 4.4.4. However, by following the link provided on that page to https://github.com/npm/npm, I found version 4.5.0 listed as the latest release. Can we assume that 4.4.4 is a ...

Is pl/pgsql block code supported by postgres-nodejs?

I am attempting to define a custom UUID variable that can be utilized across multiple queries within a transaction. Initially, I attempted using a JavaScript variable which ultimately defeated the purpose of declaring the variable on the server side. The ...

The req.ip in Express appears to alternate between displaying my local machine address as ::ffff:127.0.0.1 and ::1 in an unpredictable manner

Simply put, the title sums it up. I've spent the last few hours working on a middleware function that uses cookies for authentication, like so: const authRoute = async (req, res, next) => { console.log(req.ip); // additional logic here ...

Need a module from the main directory

Imagine having these folders: 'C:\\src' // Main directory. 'C:\\src\\inner1' // Contains 'a.js' 'C:\\src\\inner2\\innermost' // Contains 'b.js' ...

Tips for creating an Enumerable 'similarity' search using linq.js

I have a JSON array that looks like this: [ { "_Id": "0001", "_PatentId": "0000", "_Text": "Employee", "_Value": "employee", "_PermissionLevel": 55 }, { "_Id": "0002", "_PatentId": "0000", "_Text": "Employees" ...

Execute function upon initial user interaction (click for desktop users / tap for mobile users) with the Document Object Model (DOM)

Looking to trigger a function only on the initial interaction with the DOM. Are there any vanilla JavaScript options available? I've brainstormed this approach. Is it on track? window.addEventListener("click", function onFirstTouch() { console.l ...

"The use of objects as a React child is not permitted" and code linters

I'm encountering an issue with a simple component and I can't figure out why. The error message and code snippet are as follows: Error: Objects are not valid as a React child (found: object with keys {Cfor, children}). If you meant to render a ...

Extracting Ajax data - iterating through each piece of information

I am facing an issue with my ajax code. I have a collection of checkboxes, where the goal is to insert the data from the selected checkboxes into the database. When I choose just one checkbox, the insertion works fine. However, if I select multiple checkb ...

Getting JSON or JSONP data through a XAMPP local server is a straightforward process

After setting up a local server with XAMPP, my goal is to retrieve JSON / JSONP data from that server. Additional query: Do I need to upload the JSON file directly onto the server? Or can I achieve this using somePHPcoding? If so, which? I have come ac ...