使用 Amplify 集成构建 AWS Amplify React 应用程序时总是缺少 aws-exports.js [英] Missing aws-exports.js always when building AWS Amplify React app with Amplify integrations

查看:35
本文介绍了使用 Amplify 集成构建 AWS Amplify React 应用程序时总是缺少 aws-exports.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 React 应用程序使用 GraphQL API、存储、身份验证、函数、托管 - 所有有趣的东西 - 所以我必须有一个 aws-exports.js 文件可用.使用 Amplify Backend 资源放大 React 前端.

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 基本上设置为:

Repo basically setup as:

package.json
src/
   - aws-exports.js
   - app.js
   - ...etc

并在每个目录中执行 ls 表明构建时没有生成 aws-exports.js 文件.

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.

当我签入 amplify.yml 并在 Web 控制台中配置 .yml 时,情况就是如此.

This is so when i have a checked in amplify.yml and also configuring the .yml in the web console.

我试过 amplify push; 但正如预期的那样遇到

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

也在尝试:amplify pull;执行命令: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.

考虑到所有后端集成,似乎 amplify 应该自己处理这一代 aws-exports.js.当 ls 不同时.有许多关于此的问题非常流行,但没有真正的答案.感谢您的时间

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

推荐答案

我的解决方案是在npm run build"之前通过脚本简单地生成 aws-exports.js步骤.

My solution was to simply generate aws-exports.js via script before the "npm run build" step.

您只需将 aws-exports.js 内容存储在名为secretfile"的环境变量中,然后像这样在 amplify.yml 中使用它

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/**/*

原因:

  1. 将 aws-exports.js 提交到存储库当然是一个很大的禁忌,因为它包含 API 密钥和其他秘密.
  2. 我也不想每次都启动后端构建.构建后端会适得其反,因为它会为每次构建创建一个新的后端堆栈,这会花费更多资金、进一步减慢速度,而且容易出错.

谢谢.

这篇关于使用 Amplify 集成构建 AWS Amplify React 应用程序时总是缺少 aws-exports.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆