Skip to content

Why is Amplify such a mess with typescript?

0

What the h*ll (really I have to censor words as well?) are you guys doing? I am looking at the next default (easiest) aws-amplify sample on github https://github.com/aws-samples/amplify-next-template/tree/main

First weird thing with typescript, in the tsconfig.json https://github.com/aws-samples/amplify-next-template/blob/main/tsconfig.json the entire amplify folder is excluded. Ok already very strange, since there are ts files in that folder, in fact they are all typescript files. wtf.png?

The most beautiful thing that I didn't even know was possible until I saw it done in the repo: https://github.com/aws-samples/amplify-next-template/blob/main/amplify/backend.ts, in the backend.ts file other 2 ts files are being imported as .js files.

All I was trying to do, is deploy an app that simply, simply made an API call, this call might sometimes stay up for more than 60 seconds before coming back, that was all I wanted to do, to make use of AWS 15 minutes of uptime on API calls, and now I am 4 days later looking at some inexplicable code.

Please, can you send me the address of whoever's idea it was to import ts files as js files? https://github.com/swaminator is that you?

asked a year ago705 views
2 Answers
1

The issues you're encountering with TypeScript in AWS Amplify Gen 2 are related to how Amplify handles TypeScript configurations and module resolution.

Regarding the exclusion of the amplify directory in tsconfig.json: This is actually intentional and recommended. The amplify directory contains placeholder modules that are injected at build time by Amplify. When your framework's TypeScript configuration picks up the amplify directory, it tries to resolve it as a module, which can cause the "Cannot find module $amplify/env/<function-name>" error during frontend builds. Excluding the amplify directory in tsconfig.json prevents these resolution errors.

As for importing TypeScript files as JavaScript files: This is related to how Amplify handles TypeScript compilation. Amplify performs its own type-checking on the backend using a localized tsconfig.json within the amplify directory. The imports using .js extensions are a pattern used because at runtime, the TypeScript files are compiled to JavaScript, and the imports need to reference the compiled JavaScript files.

For your specific use case of deploying an app with API calls that might exceed 60 seconds, Amplify does support longer execution times (up to 15 minutes as you mentioned), but the configuration can be complex due to how the TypeScript setup works.

If you're working in a project where you need to share code between frontend and backend, you might consider moving your backend to its own package and exporting the Schema and outputs for easier sharing with your frontend application.
Sources
Troubleshoot "Cannot find module $amplify/env/<function-name>" - AWS Amplify Gen 2 Documentation
Troubleshoot "Cannot find module $amplify/env/<function-name>" - AWS Amplify Gen 2 Documentation
Separate frontend and backend teams - AWS Amplify Gen 2 Documentation

answered a year ago
EXPERT
reviewed a year ago
1

I still can't deploy my app, I am getting errors of the sort: 2025-06-25T07:11:30.464Z [INFO]: # Executing command: npx ampx pipeline-deploy --branch $AWS_BRANCH --app-id $AWS_APP_ID

105

2025-06-25T07:11:34.048Z [INFO]: 7:11:33 AM Synthesizing backend...

106

2025-06-25T07:11:34.053Z [INFO]:

107

2025-06-25T07:11:34.400Z [INFO]: 7:11:34 AM ✔ Backend synthesized in 0.46 seconds

108

2025-06-25T07:11:34.400Z [INFO]: 7:11:34 AM Running type checks...

109

2025-06-25T07:11:35.069Z [INFO]: 7:11:35 AM ✔ Type checks completed in 0.66 seconds

110

2025-06-25T07:11:35.069Z [INFO]:

111

2025-06-25T07:11:35.176Z [INFO]:

112

2025-06-25T07:11:35.177Z [INFO]: [BackendBuildError] Unable to deploy due to CDK Assembly Error

113

                             ∟ Caused by: [AssemblyError] Assembly builder failed

114

                             ∟ Caused by: [Error] Cannot find module '/codebuild/output/src3034228947/src/Dressed-by-AI/amplify/data/resource.js' imported from /codebuild/output/src3034228947/src/Dressed-by-AI/amplify/backend.ts

115

                             Resolution: Check the Caused by error and fix any issues in your backend code

116

2025-06-25T07:11:35.177Z [WARNING]: ampx pipeline-deploy

117

                                Command to deploy backends in a custom CI/CD pipeline. This command is not inten

118

                                ded to be used locally.

119

                                Options:

120

                                --debug            Print debug logs to the console

121

                                [boolean] [default: false]

122

                                --branch           Name of the git branch being deployed

123

                                [string] [required]

124

                                --app-id           The app id of the target Amplify app[string] [required]

125

                                --outputs-out-dir  A path to directory where amplify_outputs is written. I

126

                                f not provided defaults to current process working dire

127

                                ctory.                                         [string]

128

                                --outputs-version  Version of the configuration. Version 0 represents clas

129

                                sic amplify-cli config file amplify-configuration and 1

130

                                represents newer config file amplify_outputs

131

                                [string] [choices: "0", "1", "1.1", "1.2", "1.3", "1.4"] [default: "1.4"]

132

                                --outputs-format   amplify_outputs file format

133

                                [string] [choices: "mjs", "json", "json-mobile", "ts", "dart"]

134

                                -h, --help             Show help                                     [boolean]

135

2025-06-25T07:11:35.251Z [ERROR]: !!! Build failed

136

2025-06-25T07:11:35.252Z [INFO]: Please read more about Amplify Hosting's support for SSR frameworks to find if your build failure is related to an unsupported feature: https://docs.aws.amazon.com/amplify/latest/userguide/amplify-ssr-framework-support.html. You may also find this troubleshooting guide useful: https://docs.aws.amazon.com/amplify/latest/userguide/troubleshooting-ssr-deployment.html

137

2025-06-25T07:11:35.252Z [ERROR]: !!! Error: Command failed with exit code 1

138

2025-06-25T07:11:35.252Z [INFO]: # Starting environment caching...

139

2025-06-25T07:11:35.252Z [INFO]: # Environment caching completed

140

141

Absolutely insane stuff, I could have started and finished entire projects in the time it took me to try and understand this, and it still doesn't work.

answered a year 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.