- Newest
- Most votes
- Most comments
Based on the error message, it looks like AWS Lambda can't find the 'openai' module in its environment.
AWS Lambda layers are a distribution mechanism for libraries, custom runtimes, and other function dependencies. The hierarchy of the files and directories must be structured correctly for Lambda to recognize it. The 'nodejs' directory should be the parent directory for all your node modules. You should organize your layer as follows:
nodejs/node_modules/...
To resolve this issue, follow these steps:
-
Install your dependencies locally: Create a new folder, navigate into it, and then run
npm init -yto create a new Node.js project. Then, install the 'openai' module withnpm install openai. -
Prepare your Layer: After you've installed your dependencies, your directory structure should look something like this:
your_folder/ node_modules/ package.json package-lock.jsonNow, add a 'nodejs' directory at the root of your folder and move your 'node_modules', 'package.json', and 'package-lock.json' inside it. Your directory structure should now look like this:
your_folder/ nodejs/ node_modules/ package.json package-lock.json -
Create a zip file: Archive your 'nodejs' folder into a .zip file (e.g.,
layer.zip). -
Upload your Layer: Go to the AWS Lambda console, create a new Layer, and upload your .zip file. Make sure you select the correct runtime (e.g., Node.js 18.x).
-
Attach the Layer to your Lambda function: In your Lambda function configuration, add the Layer you just created.
After following these steps, your function should be able to import and use the 'openai' module. Make sure your function's code is requiring the 'openai' module correctly:
const openai = require('openai');
Finally, re-deploy your function and test it again. If everything has been set up correctly, your function should now work as expected. If you continue to experience problems, verify that your function and the Layer have the same Node.js runtime version.
If the answer is helpful, please click "Accept Answer" and upvote it.
I assume you have been following this guide: https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html.
That error may indicate that the layer was not properly created. Could be that you have not added a “nodejs” folder as part of the zip, for instance.
Relevant content
asked 4 years ago
asked 2 years ago
asked 2 years ago

Thank you so much. There is no error with cannot found module !