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?