my React app uses GraphQL API, Storage, Auth, Functions, Hosting - all the fun stuff - so i must have an aws-exports.js
file available. Amplify React Front end with Amplify Backend resources.
Repo basically setup as:
package.json
src/
- aws-exports.js
- app.js
- ...etc
and doing an ls
in each dir showed on builds that there was no aws-exports.js
file generated.
With many different configs, i am met with:
[INFO]: # Executing command: yarn run build
[INFO]: yarn run v1.16.0
[INFO]: $ react-scripts build
[INFO]: Creating an optimized production build...
[INFO]: Failed to compile.
[INFO]: ./src/App.js
Cannot find file './aws-exports' in './src'.
2020-04-30T00:52:34.883Z [WARNING]: error Command failed with exit code 1.
This is so when i have a checked in amplify.yml
and also configuring the .yml
in the web console.
I've tried amplify push;
but as expected met with
An error occured during the push operation: Current environment cannot be determined
Use 'amplify init' in the root of your app directory to initialize your project with Amplify
Also trying: amplify pull;
or Executing command: amplify pull --appId abc123abc123 --envName dev
# Starting phase: preBuild
# Executing command: amplify pull
For more information on AWS Profiles, see: https://docs.aws.amazon.com/cli/latest/userguide/cli-multiple-profiles.html
? Do you want to use an AWS profile? (Y/n)
.[43D.[43C
Which just hangs and expects input. I dont think manually putting in creds like this is as all the way to go about this.
It seems as though amplify should handle this generation of aws-exports.js
itself considering all the backend integrations. When ls
different. There are a number of questions around on this that are quite current but with no real answer. Thanks for your time
aws-exports. This file is generated only for JavaScript projects. It contains the consolidated outputs from all the categories and is placed under the src directory specified during the init process. It is updated after amplify push . This file is consumed by the Amplify JavaScript library for configuration.
To autogenerate aws-exports.js at build-time Sign in to the AWS Management Console and open the Amplify console . Choose the app to edit. Choose the Frontend environments tab. Locate the branch to edit and choose Edit.
You have deployed a React application in the AWS Cloud by integrating with GitHub and using AWS Amplify. With AWS Amplify, you can continuously deploy your application in the cloud and host it on a globally-available CDN. Next, we will create a local version of the app to continue development and add new features.
The error "could not find declaration file for module 'react'" occurs when TypeScript cannot find the type declaration for a react-related module. To solve the error install the types for the module by running the command from the error message, e.g. npm install -D @types/react .
My solution was to simply generate aws-exports.js via script before the "npm run build" step.
You simply store aws-exports.js contents in an environment variable called "secretfile", and then use it inside amplify.yml like this
version: 1
frontend:
phases:
preBuild:
commands:
- npm ci
build:
commands:
- echo $secretfile > ./src/aws-exports.js
- npm run build
artifacts:
baseDirectory: build
files:
- '**/*'
cache:
paths:
- node_modules/**/*
Reasons:
Thanks.
Backend resources need amplifyPush
run to generate the expected aws-exports.js
file. A normal react+amplify backend project will need a build script looking like:
version: 0.1
env:
variables:
key: value
backend:
phases:
build:
commands:
- amplifyPush --simple
frontend:
phases:
preBuild:
commands:
- yarn install
build:
commands:
- yarn run build
artifacts:
baseDirectory: build
files:
- "**/*"
cache:
paths:
- node_modules/**/*
The amplifyPush
script is a part of the amplify-console repo, specifically the .sh script is found https://github.com/aws-amplify/amplify-console/blob/master/scripts/amplifyPush.sh
See here for more info on other things to run in your build script.
https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html#frontend-with-backend
For anyone who lands here, the Amplify console just released a way so that you can autogenerate the aws-exports file at build time without enabling fullstack CI/CD and checking it into your git repo: https://docs.aws.amazon.com/amplify/latest/userguide/amplify-config-autogeneration.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With