aws-sam-cli: Bug: Invoking a lambda function is giving me an error 'cannot find module' even though module exists

After building and starting the local sam server, I’m noticing after I run my lambda GetBlocksByDateRangeFunction, I get this error in the console.

Invalid lambda response received: Invalid API Gateway Response Keys: {'trace', 'errorMessage', 'errorType'} in {'errorType': 'Runtime.ImportModuleError', 'errorMessage': "Error: Cannot find
module 'something'\nRequire stack:\n- /var/runtime/index.mjs", 'trace': ["Runtime.ImportModuleError: Error: Cannot find module 'something'", 'Require stack:', '- /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']}

Steps to reproduce:

I’m not sure how I got this behavior to happen.

Observed result:

2023-06-25 11:58:45,415 | Constructed Event 1.0 to invoke Lambda. Event: {'version': '1.0', 'httpMethod': 'GET', 'body': None, 'resource': '/kevin', 'requestContext': {'resourceId': '123456',
'apiId': '1234567890', 'resourcePath': '/kevin', 'httpMethod': 'GET', 'requestId': '04e1e7ee-a920-462e-ac8d-3589db2ff535', 'accountId': '123456789012', 'stage': 'Prod', 'identity': {'apiKey':
None, 'userArn': None, 'cognitoAuthenticationType': None, 'caller': None, 'userAgent': 'Custom User Agent String', 'user': None, 'cognitoIdentityPoolId': None, 'cognitoAuthenticationProvider':
None, 'sourceIp': '127.0.0.1', 'accountId': None}, 'extendedRequestId': None, 'path': '/kevin', 'protocol': 'HTTP/1.1', 'domainName': '127.0.0.1:3001', 'requestTimeEpoch': 1687719488,
'requestTime': '25/Jun/2023:18:58:08 +0000'}, 'queryStringParameters': None, 'multiValueQueryStringParameters': None, 'headers': {'User-Agent': 'PostmanRuntime/7.32.3', 'Accept': '*/*',
'Postman-Token': '6f0a0ea9-75de-4839-a840-947f37c83019', 'Host': '127.0.0.1:3001', 'Accept-Encoding': 'gzip, deflate, br', 'Connection': 'keep-alive', 'X-Forwarded-Proto': 'http',
'X-Forwarded-Port': '3001'}, 'multiValueHeaders': {'User-Agent': ['PostmanRuntime/7.32.3'], 'Accept': ['*/*'], 'Postman-Token': ['6f0a0ea9-75de-4839-a840-947f37c83019'], 'Host':
['127.0.0.1:3001'], 'Accept-Encoding': ['gzip, deflate, br'], 'Connection': ['keep-alive'], 'X-Forwarded-Proto': ['http'], 'X-Forwarded-Port': ['3001']}, 'pathParameters': None,
'stageVariables': None, 'path': '/kevin', 'isBase64Encoded': False}
2023-06-25 11:58:45,419 | Found one Lambda function with name 'GetBlocksByDateRangeFunction'
2023-06-25 11:58:45,424 | Invoking src/handlers/something.somethingHandler (nodejs18.x)
2023-06-25 11:58:45,425 | No environment variables found for function 'GetBlocksByDateRangeFunction'
2023-06-25 11:58:45,426 | Resolving code path. Cwd=/Users/kirkchu/pairup/ourtracks-node-api/.aws-sam/build,
CodeUri=/Users/kirkchu/pairup/ourtracks-node-api/.aws-sam/build/GetBlocksByDateRangeFunction
2023-06-25 11:58:45,427 | Resolved absolute path to code is /Users/kirkchu/pairup/ourtracks-node-api/.aws-sam/build/GetBlocksByDateRangeFunction
2023-06-25 11:58:45,427 | Code /Users/kirkchu/pairup/ourtracks-node-api/.aws-sam/build/GetBlocksByDateRangeFunction is not a zip/jar file
2023-06-25 11:58:45,465 | Using local image: public.ecr.aws/lambda/nodejs:18-rapid-x86_64.

2023-06-25 11:58:45,466 | Mounting /Users/kirkchu/pairup/ourtracks-node-api/.aws-sam/build/GetBlocksByDateRangeFunction as /var/task:ro,delegated, inside runtime container
2023-06-25 11:58:46,369 | Starting a timer for 3 seconds for function 'GetBlocksByDateRangeFunction'
START RequestId: 56b06cbc-5a48-4570-886f-f9e589e8b906 Version: $LATEST
2023-06-25T18:58:46.658Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'something'\nRequire stack:\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'something'","Require stack:","- /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"]}
25 Jun 2023 18:58:46,687 [ERROR] (rapid) Init failed error=Runtime exited with error: exit status 129 InvokeID=
2023-06-25T18:58:46.957Z	undefined	ERROR	Uncaught Exception 	{"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'something'\nRequire stack:\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'something'","Require stack:","- /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"]}
END RequestId: 191afcfa-7165-4e0d-a437-efa1c55904c9
REPORT RequestId: 191afcfa-7165-4e0d-a437-efa1c55904c9	Init Duration: 0.94 ms	Duration: 567.76 ms	Billed Duration: 568 ms	Memory Size: 128 MB	Max Memory Used: 128 MB
2023-06-25 11:58:47,270 | Cleaning all decompressed code dirs
2023-06-25 11:58:47,272 | Lambda returned empty body!
2023-06-25 11:58:47,273 | Invalid lambda response received: Invalid API Gateway Response Keys: {'errorType', 'trace', 'errorMessage'} in {'errorType': 'Runtime.ImportModuleError',
'errorMessage': "Error: Cannot find module 'something'\nRequire stack:\n- /var/runtime/index.mjs", 'trace': ["Runtime.ImportModuleError: Error: Cannot find module 'something'", 'Require
stack:', '- /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']}

Expected result:

I’m expecting it to not return an error and not to say Cannot Find Module since the module does exist.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

Mac

  1. OS: MacOS Big Sur
  2. sam --version: SAM CLI, version 1.88.0
  3. AWS region: us-west-2
# Paste the output of `sam --info` here
{
  "version": "1.88.0",
  "system": {
    "python": "3.11.4",
    "os": "macOS-11.2.2-x86_64-i386-64bit"
  },
  "additional_dependencies": {
    "docker_engine": "20.10.23",
    "aws_cdk": "Not available",
    "terraform": "Not available"
  },
  "available_beta_feature_env_vars": [
    "SAM_CLI_BETA_FEATURES",
    "SAM_CLI_BETA_BUILD_PERFORMANCE",
    "SAM_CLI_BETA_TERRAFORM_SUPPORT",
    "SAM_CLI_BETA_RUST_CARGO_LAMBDA"
  ]
}```

`Add --debug flag to command you are running`

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 21 (2 by maintainers)

Most upvoted comments

This error happens to me on the sample project:

  • hello world
  • node18 & node16 (others i did not try)
  • hello world typescript

No problem invoking and run the api for the plain js version

Also seeing this error on Mac M1 with this starter template. I previously also got it on the plain JS template, but not after updating the CLI to the latest version.

A workaround for me seems to be running sam build manually before running sam local start-api - then it seems to work

I got it working by following the steps:

  1. sam build
  2. sam local start-api

This issue occurring when using hello world example with typescript.

I got it working by following the steps:

  1. sam build
  2. sam local start-api

This issue occurring when using hello world example with typescript.

This did it for me, thank you @developerKumar!

In my case above, I discovered that I had an outdated version of the SAM CLI that was installed via homebrew, in addition to the one I’d installed via the MacOS package installer.

After uninstalling both, and installing the latest (v1.97.0), debugging my lambda function works nicely for me.