Skip to content

"errorType": "Runtime.ImportModuleError",

0

I'm making on

  • AWS Lambda
  • add Layers (node_modules , package.json , package-lock.json)
  • add API Gateway(POST method)

I got a error

Response
{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'openai'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'openai'",
    "Require stack:",
    "- /var/task/index.js",
    "- /var/runtime/index.mjs",
    "    at _loadUserApp (file:///var/runtime/index.mjs:997:17)",
    "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)",
    "    at async start (file:///var/runtime/index.mjs:1195:23)",
    "    at async file:///var/runtime/index.mjs:1201:1"
  ]
}

I reinstalled openai then upload to Layers. I changed Layers to Last version(what I reinstalled openai). but I still got this error. What can I do now? What can I check?

asked 3 years ago7.8K views

2 Answers
1

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:

  1. Install your dependencies locally: Create a new folder, navigate into it, and then run npm init -y to create a new Node.js project. Then, install the 'openai' module with npm install openai.

  2. 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.json
    

    Now, 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
    
  3. Create a zip file: Archive your 'nodejs' folder into a .zip file (e.g., layer.zip).

  4. 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).

  5. 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.

EXPERT

answered 3 years ago

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

1

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.

EXPERT

answered 3 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.