aws-sam-cli: Cannot find module 'app' when using "hello world typescript" example
Hello đź‘‹
I’m setting up AWS SAM locally on Windows and I’ve stumbled upon a problem with running a lambda locally. I’ve used the “Hello World TypeScript” as an example and haven’t changed almost anything and I’m receiving the following error:
[ERROR] (rapid) Invoke DONE failed: Sandbox.Failure
{"errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/index.mjs", "trace": ["Runtime.ImportModuleError: Error: Cannot find module 'app'", "Require stack:", "- /var/runtime/index.mjs", " at _loadUserApp (file:///var/runtime/index.mjs:1087:17)", " at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)", " at async start (file:///var/runtime/index.mjs:1282:23)", " at async file:///var/runtime/index.mjs:1288:1"]}
Full debug log:
PS D:\Projects\testApp\server> sam local invoke HelloWorldFunction --debug
2024-07-22 01:21:14,070 | Config file location: D:\Projects\testApp\server\samconfig.toml
2024-07-22 01:21:14,072 | Loading configuration values from [default.['local', 'invoke'].parameters]
(env.command_name.section) in config file at 'D:\Projects\testApp\server\samconfig.toml'...
2024-07-22 01:21:14,073 | Configuration values successfully loaded.
2024-07-22 01:21:14,073 | Configuration values are: {'stack_name': 'testApp-server'}
2024-07-22 01:21:14,077 | Using SAM Template at D:\Projects\testApp\server\.aws-sam\build\template.yaml
2024-07-22 01:21:14,088 | Using config file: samconfig.toml, config environment: default
2024-07-22 01:21:14,089 | Expand command line arguments to:
2024-07-22 01:21:14,089 | --template_file=D:\Projects\testApp\server\.aws-sam\build\template.yaml
--function_logical_id=HelloWorldFunction --no_event --layer_cache_basedir=C:\xx\xx\AppData\Roaming\AWS
SAM\layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2024-07-22 01:21:14,090 | local invoke command is called
2024-07-22 01:21:14,158 | No current session found, using default AWS::AccountId
2024-07-22 01:21:14,162 | No Parameters detected in the template
2024-07-22 01:21:14,193 | Sam customer defined id is more priority than other IDs. Customer defined id for
resource HelloWorldFunction is HelloWorldFunction
2024-07-22 01:21:14,194 | Sam customer defined id is more priority than other IDs. Customer defined id for
resource TestFunction is TestFunction
2024-07-22 01:21:14,195 | Sam customer defined id is more priority than other IDs. Customer defined id for
resource TestFunctionJS is TestFunctionJS
2024-07-22 01:21:14,196 | There is no customer defined id or cdk path defined for resource
ServerlessRestApi, so we will use the resource logical id as the resource id
2024-07-22 01:21:14,197 | 0 stacks found in the template
2024-07-22 01:21:14,197 | No Parameters detected in the template
2024-07-22 01:21:14,207 | Sam customer defined id is more priority than other IDs. Customer defined id for
resource HelloWorldFunction is HelloWorldFunction
2024-07-22 01:21:14,208 | Sam customer defined id is more priority than other IDs. Customer defined id for
resource TestFunction is TestFunction
2024-07-22 01:21:14,208 | Sam customer defined id is more priority than other IDs. Customer defined id for
resource TestFunctionJS is TestFunctionJS
2024-07-22 01:21:14,209 | There is no customer defined id or cdk path defined for resource
ServerlessRestApi, so we will use the resource logical id as the resource id
2024-07-22 01:21:14,210 | 4 resources found in the stack
2024-07-22 01:21:14,210 | Found Serverless function with name='HelloWorldFunction' and
CodeUri='HelloWorldFunction'
2024-07-22 01:21:14,211 | --base-dir is not presented, adjusting uri HelloWorldFunction relative to
D:\Projects\testApp\server\.aws-sam\build\template.yaml
2024-07-22 01:21:14,212 | Found Serverless function with name='TestFunction' and CodeUri='TestFunction'
2024-07-22 01:21:14,212 | --base-dir is not presented, adjusting uri TestFunction relative to
D:\Projects\testApp\server\.aws-sam\build\template.yaml
2024-07-22 01:21:14,213 | Found Serverless function with name='TestFunctionJS' and CodeUri='TestFunctionJS'
2024-07-22 01:21:14,213 | --base-dir is not presented, adjusting uri TestFunctionJS relative to
D:\Projects\testApp\server\.aws-sam\build\template.yaml
2024-07-22 01:21:14,218 | Found one Lambda function with name 'HelloWorldFunction'
2024-07-22 01:21:14,218 | Invoking app.lambdaHandler (nodejs20.x)
2024-07-22 01:21:14,219 | Loading AWS credentials from session with profile 'None'
2024-07-22 01:21:14,225 | Resolving code path. Cwd=D:\Projects\testApp\server\.aws-sam\build,
CodeUri=D:\Projects\testApp\server\.aws-sam\build\HelloWorldFunction
2024-07-22 01:21:14,226 | Resolved absolute path to code is
D:\Projects\testApp\server\.aws-sam\build\HelloWorldFunction
2024-07-22 01:21:14,227 | Resolving code path. Cwd=D:\Projects\testApp\server\.aws-sam\build,
CodeUri=D:\Projects\testApp\server\.aws-sam\build\HelloWorldFunction
2024-07-22 01:21:14,227 | Resolved real code path to
D:\Projects\testApp\server\.aws-sam\build\HelloWorldFunction
2024-07-22 01:21:14,228 | Code D:\Projects\testApp\server\.aws-sam\build\HelloWorldFunction is not a
zip/jar file
2024-07-22 01:21:16,928 | Local image is up-to-date
2024-07-22 01:21:16,931 | Checking free port on 127.0.0.1:7773
2024-07-22 01:21:16,933 | Using local image: public.ecr.aws/lambda/nodejs:20-rapid-x86_64.
2024-07-22 01:21:16,934 | Mounting D:\Projects\testApp\server\.aws-sam\build\HelloWorldFunction as
/var/task:ro,delegated, inside runtime container
2024-07-22 01:21:17,224 | Starting a timer for 3 seconds for function 'HelloWorldFunction'
2024-07-22 01:21:17,226 | Getting lock for the key localhost-7773
2024-07-22 01:21:17,226 | Waiting to retrieve the lock (localhost-7773) to start invocation
START RequestId: d337cb92-eafa-4ddc-ac0c-3d5dfbb7ce27 Version: $LATEST
2024-07-21T23:21:18.277Z undefined ERROR Uncaught Exception {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'app'","Require stack:","- /var/runtime/index.mjs"," at _loadUserApp (file:///var/runtime/index.mjs:1087:17)"," at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)"," at async start (file:///var/runtime/index.mjs:1282:23)"," at async file:///var/runtime/index.mjs:1288:1"]}
21 Jul 2024 23:21:18,286 [ERROR] (rapid) Init failed error=Runtime exited with error: exit status 129 InvokeID=
21 Jul 2024 23:21:18,286 [ERROR] (rapid) Invoke failed error=Runtime exited with error: exit status 129 InvokeID=a4dea20a-1ea1-4ead-971e-4e49bf151570
21 Jul 2024 23:21:18,286 [ERROR] (rapid) Invoke DONE failed: Sandbox.Failure
{"errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/index.mjs", "trace": ["Runtime.ImportModuleError: Error: Cannot find module 'app'", "Require stack:", "- /var/runtime/index.mjs", " at _loadUserApp (file:///var/runtime/index.mjs:1087:17)", " at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1119:21)", " at async start (file:///var/runtime/index.mjs:1282:23)", " at async file:///var/runtime/index.mjs:1288:1"]}
I have ran the sam build function before running the function, the build was successfully created.
Would appreciate guidance as I did not have time to fully investigate this just yet, didn’t bother especially that this might be a known problem. The path to the “module” which is 'app' does not seem correct though 🤔
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 15 (5 by maintainers)
Got same issue but on Mac (arm), some details in #7845 (closed it as duplicate for this one)
Sorry @doteric for the late reply, I don’t have any particularly elegant workaround right now, but the workaround I do have involves using SAM CLI inside of WSL2, instead of on Windows.
credsStorekey and value from the~/.docker/config.jsonfilecdinto/mnt/c/users/administrator/desktop/sam-appThis can be a potential workaround for now while our team prioritizes a fix/the best way forward.
Thanks! I switched over to Rancher Desktop and can now reproduce the problem, we’re making progress! It looks like this may have something to do with how both flavours deal with bind mounts.
SAM CLI passes the source directory as a folder that is on the Windows machine (eg.
C:\my\cool\path), and the destination as a path in the container (eg./var/task). We use a Docker API library to communicate with the daemon, and it looks like on both Docker and Rancher Desktop, the windows path is correctly converted to something POSIX like (eg./c/my/cool/path), but only Docker Desktop is able to handle this path and mount the folder. Rancher Desktop won’t bind mount correctly.This is a little weird, since the actual path on WSL is
/mnt/c/.... If I was do manually rundocker run -v C:\my\cool\path:/var/path ...on Rancher, it will work. I don’t have a workaround right now, but we’ll continue to look into it.