简体   繁体   中英

How do I set up my build scripts in AWS Amplify? They run successfully locally and fail in the build process for AWS Amplify

I am running my build scripts locally with NPM and they complete successfully. However, I am hosting my website with AWS Amplify, have accepted the default build settings per their recommendation but the build always fails on the during the frontend build.

I've read through the documentation ( https://aws.amazon.com/getting-started/tutorials/deploy-react-app-cicd-amplify/ )

Here is my build script in 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"

''''

Here is the default build file in 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/**/*

''''

Here is the output of the Amplify FrontEnd build process: ''''

               # 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

....

The AWS Amplify build should complete successfully, but it always fails.

I used the following to get my build on AWS amplify. Follow the order of these steps, try the next if you fail.

First of all its recommended to use the same package manager for your build that you use in your project. Anyways try these:

  1. try deleting and re-installing node_modules .
  2. try building the project with webpack version(3.1.0)/webpack-server-version(1)
  3. try deleting and re-installing react-scripts.

I had a similar issue, my package.json contained a postinstall script which was trying to serve the site locally for development. After minifying the assets, the Amplify build log was timing out and failing for seemingly no reason.

My solution was to remove the postinstall script, as it was not required for deploying.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM