Runtime.ImportModuleError: Error: Cannot find module '@aws-sdk/client-sqs' error on Function (graphql)

the graphql Function got Runtime.ImportModuleError: Error: Cannot find module '@aws-sdk/client-sqs' error for https://staging--utopic-ai.netlify.app/, and the graphql endpoint was broken.

I have

  • updated the aws-sdk to v3: “@aws-sdk/client-sqs”: “^3.363.0”
  • added
[functions]
  external_node_modules = ["@aws-sdk/client-sqs"]
  • node version=v16.14.2
  • yarn version=3.4.1

Hiya welcome to the forums! Thanks so much for reaching out.

your import statement look shoudl look a little like this.

import { SQSClient } from '@aws-sdk/client-sqs'

we do have import { SQSClient, SendMessageCommand } from '@aws-sdk/client-sqs'

instead, add:

[functions]
  included_files = ["./node_modules/@aws-sdk/**/*"]
0: "Runtime.ImportModuleError: Error: Cannot find module '@smithy/protocol-http'"
1: "Require stack:"
2: "- /var/task/node_modules/@aws-sdk/client-sqs/node_modules/@aws-sdk/middleware-host-header/dist-cjs/index.js"
3: "- /var/task/node_modules/@aws-sdk/client-sqs/dist-cjs/SQSClient.js"
4: "- /var/task/node_modules/@aws-sdk/client-sqs/dist-cjs/index.js"
5: "- /var/task/api/dist/services/tokenTransferRequests/tokenTransferRequests.js"
6: "- /var/task/api/dist/functions/graphql.js"
7: "- /var/task/graphql.js"
8: "- /var/runtime/index.mjs"
9: "    at _loadUserApp (file:///var/runtime/index.mjs:997:17)"
10: "    at async Object.UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)"
11: "   at async start (file:///var/runtime/index.mjs:1195:23)"
12: "    at async file:///var/runtime/index.mjs:1201:1"

Got this error after added your change. @hrishikesh

Hi, i’m seeing your most recent branch deploy was successful. Please reach back out if you are still experiencing this issue.

it’s a runtime error, but build time error @SamO

You might have to add that module in a similar fashion to the included_files.

@hrishikesh why do we need to include this in the .toml file if it should be installed using package.json?

You still need to install it using package.json. If you don’t add it there, adding this to the toml won’t do anything.

To be honest, you didn’t reply to the original question, so let me please ask it again:

Why do we need to set anything in toml file (like include_files = ["./node_modules/@aws-sdk/**/*"]) when all the needed dependencies (aws-sdk/***) are installed locally in the node_modules and present in package.json?

What is the reason Netlify function correctly works with any other dependency but not with @aws-sdk?

Yeah, i know… but why we need to add in toml?
If i add aws-sdk in toml, the error is Cannot find module '@smithy/protocol-http, then when we add smithy protocol http, it asks to add another module, then another module, then another module

it doesn’t seem right

One of your dependencies is not importing or loading the modules in a way that it gets bundled during the runtime. Thus the issue.

Or it could also be due to: @aws-lambda-nodejs: missing aws-sdk-v3 libs at runtime · Issue #23873 · aws/aws-cdk · GitHub