如何在AWS Amplify中设置构建脚本?它们在本地成功运行,并且在AWS Amplify的构建过程中失败

时间:2019-05-12 11:27:16

标签: amazon-web-services npm deployment aws-amplify

我正在使用NPM在本地运行构建脚本,它们已成功完成。但是,我使用AWS Amplify托管我的网站,已经按照他们的建议接受了默认构建设置,但是在前端构建期间构建始终会失败。

我已经阅读了文档(https://aws.amazon.com/getting-started/tutorials/deploy-react-app-cicd-amplify/

这是我在package.json中的构建脚本: ''''

 "scripts": {
    "start": "npm run watch:all",
    "test": "echo \"Error: no test specified\" && exit 1",
    "lite": "lite-server",
    "jshint": "jshint",
    "scss": "node-sass -o css/ css/",
    "watch:scss": "onchange \"css/*.scss\" -- npm run scss",
    "watch:all": "concurrently \"npm run watch:scss\" \"npm run lite\"",
    "clean": "rimraf dist",
    "copyfonts": "copyfiles -f node_modules/font-awesome/fonts/* dist/fonts",
    "imagemin": "imagemin img/* -o dist/img",
    "usemin": " usemin index.html -d dist --htmlmin -o dist/index.html &&  usemin photos.html -d dist --htmlmin -o dist/photos.html && usemin \"pico's picks\".html -d dist --htmlmin -o dist/\"pico's picks\".html && usemin about.html -d dist --htmlmin -o dist/about.html && usemin contact.html -d dist --htmlmin -o dist/contact.html",
    "build": "npm run clean && npm run copyfonts && npm run imagemin && npm run usemin"

''''

这是AWS Amplify中的默认构建文件: ''''

version: 0.1
frontend:
  phases:
    preBuild:
      commands:
        - npm ci
    build:
      commands:
        - npm run build
  artifacts:
    # IMPORTANT - Please verify your build output directory
    baseDirectory: /
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

''''

这是Amplify FrontEnd构建过程的输出: ''''

               # Starting phase: preBuild
                                 # Executing command: npm ci
2019-05-12T10:10:02.664Z [INFO]: > pngquant-bin@3.1.1 postinstall /codebuild/output/src794671044/src/Project-Pico/node_modules/pngquant-bin
                                 > node lib/install.js
2019-05-12T10:10:03.154Z [WARNING]: ✔ pngquant pre-build test passed successfully
2019-05-12T10:10:03.163Z [INFO]: > optipng-bin@3.1.4 postinstall /codebuild/output/src794671044/src/Project-Pico/node_modules/optipng-bin
                                 > node lib/install.js
2019-05-12T10:10:03.514Z [WARNING]: ✔ optipng pre-build test passed successfully
2019-05-12T10:10:03.514Z [WARNING]: 
2019-05-12T10:10:03.521Z [INFO]: > jpegtran-bin@3.2.0 postinstall /codebuild/output/src794671044/src/Project-Pico/node_modules/jpegtran-bin
                                 > node lib/install.js
2019-05-12T10:10:03.882Z [WARNING]: ✔ jpegtran pre-build test passed successfully
2019-05-12T10:10:03.883Z [WARNING]: 
2019-05-12T10:10:03.890Z [INFO]: > gifsicle@3.0.4 postinstall /codebuild/output/src794671044/src/Project-Pico/node_modules/gifsicle
                                 > node lib/install.js
2019-05-12T10:10:04.265Z [WARNING]: ✔ gifsicle pre-build test passed successfully
2019-05-12T10:10:04.374Z [INFO]: > fsevents@1.2.9 install /codebuild/output/src794671044/src/Project-Pico/node_modules/fsevents
                                 > node install
2019-05-12T10:10:04.504Z [INFO]: > node-sass@4.12.0 install /codebuild/output/src794671044/src/Project-Pico/node_modules/node-sass
                                 > node scripts/install.js
2019-05-12T10:10:05.004Z [INFO]: Downloading binary from https://github.com/sass/node-sass/releases/download/v4.12.0/linux-x64-57_binding.node
2019-05-12T10:10:05.343Z [INFO]: Download complete
2019-05-12T10:10:05.346Z [INFO]: Binary saved to /codebuild/output/src794671044/src/Project-Pico/node_modules/node-sass/vendor/linux-x64-57/binding.node
2019-05-12T10:10:05.371Z [INFO]: Caching binary to /root/.npm/node-sass/4.12.0/linux-x64-57_binding.node
2019-05-12T10:10:05.396Z [INFO]: > node-sass@4.12.0 postinstall /codebuild/output/src794671044/src/Project-Pico/node_modules/node-sass
                                 > node scripts/build.js
2019-05-12T10:10:05.526Z [INFO]: Binary found at /codebuild/output/src794671044/src/Project-Pico/node_modules/node-sass/vendor/linux-x64-57/binding.node
2019-05-12T10:10:05.527Z [INFO]: Testing binary
2019-05-12T10:10:05.623Z [INFO]: Binary is fine
2019-05-12T10:10:05.702Z [WARNING]: added 878 packages in 8.92s
2019-05-12T10:10:05.712Z [INFO]: # Completed phase: preBuild
                                 # Starting phase: build
2019-05-12T10:10:05.713Z [INFO]: # Executing command: npm run build
2019-05-12T10:10:05.901Z [INFO]: > project-pico@1.0.0 build /codebuild/output/src794671044/src/Project-Pico
                                 > npm run clean && npm run copyfonts && npm run imagemin && npm run usemin
2019-05-12T10:10:06.087Z [INFO]: > project-pico@1.0.0 clean /codebuild/output/src794671044/src/Project-Pico
                                 > rimraf dist
2019-05-12T10:10:06.342Z [INFO]: > project-pico@1.0.0 copyfonts /codebuild/output/src794671044/src/Project-Pico
                                 > copyfiles -f node_modules/font-awesome/fonts/* dist/fonts
2019-05-12T10:10:06.657Z [INFO]: > project-pico@1.0.0 imagemin /codebuild/output/src794671044/src/Project-Pico
                                 > imagemin img/* -o dist/img

....

AWS Amplify构建应该成功完成,但是总是失败。

2 个答案:

答案 0 :(得分:0)

我使用以下内容在AWS Amplify上构建了我的构建。请按照这些步骤的顺序进行操作,如果失败,请尝试下一个。

首先,建议您在项目中使用相同的程序包管理器进行构建。无论如何,请尝试以下方法:

  1. 尝试删除并重新安装node_modules。
  2. 尝试使用webpack版本(3.1.0)/ webpack-server-version(1)构建项目
  3. 尝试删除并重新安装react-scripts。

答案 1 :(得分:0)

我遇到了类似的问题,我的 package.json 包含一个安装后脚本,该脚本试图在本地为站点提供服务以进行开发。在缩小资产后,Amplify 构建日志似乎无缘无故超时并失败。

我的解决方案是删除安装后脚本,因为部署不需要它。