Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot find module 'app' when using "hello world typescript" example #7262

Open
doteric opened this issue Jul 21, 2024 · 10 comments
Open

Cannot find module 'app' when using "hello world typescript" example #7262

doteric opened this issue Jul 21, 2024 · 10 comments
Labels
area/docker area/local/invoke sam local invoke command stage/needs-investigation Requires a deeper investigation type/bug

Comments

@doteric
Copy link

doteric commented Jul 21, 2024

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 🤔

@doteric doteric added the stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. label Jul 21, 2024
@sidhujus
Copy link
Contributor

Hi @doteric, Thanks for raising this issue. Could you provide some more details such as what options you selected when running the sam init command? I haven't been able to reproduce your exact error so this would help us with that

@sidhujus sidhujus added blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. area/local/invoke sam local invoke command and removed stage/needs-triage Automatically applied to new issues and PRs, indicating they haven't been looked at. labels Jul 24, 2024
@doteric
Copy link
Author

doteric commented Jul 24, 2024

Hey @sidhujus
Thank you for the reply. Here is end generation log:

    -----------------------
    Generating application:
    -----------------------
    Name: example
    Runtime: nodejs20.x
    Architectures: x86_64
    Dependency Manager: npm
    Application Template: hello-world-typescript
    Output Directory: .
    Configuration file: example\samconfig.toml

That's that suit you or you would like something more?

@YamaguchiGen-TR
Copy link

Hi @doteric
Did you use the -t option to specify a file that is not template.yaml?
I also had the same problem error when I used the -t option.

@doteric
Copy link
Author

doteric commented Jul 25, 2024

Hey @YamaguchiGen-TR ,
No, I have not provided the -t (template file path) param when running any commands. It seems to detect the template file correctly without it and I assume you mean that you have provided it with the wrong path?
Thank you for the idea though 💪

@YamaguchiGen-TR
Copy link

Hi @doteric
I think the template file is detected correctly. If the -t option is not specified, template.[yaml|yml] in the current directory is used.Thank you for your reply!

@lucashuy
Copy link
Contributor

Hi, I unfortunately can't reproduce this with the node20 Typescript provided template.

I ran:

sam build
sam local invoke

Here is the system I tried it on:

PS C:\Users\Administrator\Desktop\sam-app> sam --info
{
  "version": "1.120.0",
  "system": {
    "python": "3.11.8",
    "os": "Windows-10-10.0.20348-SP0"
  },
  "additional_dependencies": {
    "docker_engine": "25.0.3",
    "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"
  ]
}

Docker version:

PS C:\Users\Administrator\Desktop\sam-app> docker version
Client:
 Cloud integration: v1.0.35+desktop.11
 Version:           25.0.3
 API version:       1.44
 Go version:        go1.21.6
 Git commit:        4debf41
 Built:             Tue Feb  6 21:13:02 2024
 OS/Arch:           windows/amd64
 Context:           default

Server: Docker Desktop 4.28.0 (139021)
 Engine:
  Version:          25.0.3
  API version:      1.44 (minimum version 1.24)
  Go version:       go1.21.6
  Git commit:       f417435
  Built:            Tue Feb  6 21:14:25 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.6.28
  GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12
  GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

Can you copy and paste the contents of the sam --info and docker version command here? Thanks

@doteric
Copy link
Author

doteric commented Jul 29, 2024

Hello @lucashuy
Really appreciate the contribution 🙇
Here are the results of the mentioned:

sam --info    
{
  "version": "1.120.0",
  "system": {
    "python": "3.11.8",
    "os": "Windows-10-10.0.22631-SP0"
  },
  "additional_dependencies": {
    "docker_engine": "25.0.5",
    "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"
  ]
}
docker version
Client:
 Version:           26.1.0-rd
 API version:       1.44 (downgraded from 1.45)
 Go version:        go1.21.9
 Git commit:        cca8e72
 Built:             Tue Apr 23 16:26:59 2024
 OS/Arch:           windows/amd64
 Context:           default

Server:
 Engine:
  Version:          25.0.5
  API version:      1.44 (minimum version 1.24)
  Go version:       go1.21.10
  Git commit:       e63daec8672d77ac0b2b5c262ef525c7cf17fd20
  Built:            Sun May 12 07:25:43 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.7.10
  GitCommit:        4e1fe7492b9df85914c389d1f15a3ceedbb280ac
 runc:
  Version:          1.1.12
  GitCommit:        51d5e94601ceffbbd85688df1c928ecccbfa4685
 docker-init:
  Version:          0.19.0
  GitCommit:        

Just a side node - I use Rancher Desktop for docker.

@lucashuy
Copy link
Contributor

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 run docker 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.

@lucashuy lucashuy added area/docker type/bug stage/needs-investigation Requires a deeper investigation and removed blocked/more-info-needed More info is needed from the requester. If no response in 14 days, it will become stale. labels Jul 31, 2024
@doteric
Copy link
Author

doteric commented Aug 11, 2024

Hello @lucashuy

Big thanks for picking this up, apart from using something other than Rancher Desktop is there some decent workaround that can be applied right now? Or any ETA on some fix? I'm wondering if I should change to something other than Rancher Desktop (for example Docker Desktop) or just wait for some fix.

Really appreciate your work btw, thank you 💪

@lucashuy
Copy link
Contributor

lucashuy commented Aug 13, 2024

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.

  1. Install SAM CLI inside of WSL2 using our Linux artifacts
  2. Configure Rancher Desktop to expose itself inside of WSL2 (https://docs.rancherdesktop.io/ui/preferences/wsl/integrations)
    • Rancher Desktop > Preferences > WSL > Integrations
  3. Inside of WSL2, remove the credsStore key and value from the ~/.docker/config.json file
    • I had done this since I don't have any credentials storage providers configured, this prevents the Docker daemon from erroring out
  4. Navigate to the project location from within WSL2
    • In my case, I needed to cd into /mnt/c/users/administrator/desktop/sam-app
  5. Run your regular SAM CLI commands inside of WSL2

This can be a potential workaround for now while our team prioritizes a fix/the best way forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/docker area/local/invoke sam local invoke command stage/needs-investigation Requires a deeper investigation type/bug
Projects
None yet
Development

No branches or pull requests

4 participants