본문 바로가기

Infra

AWS Amplify 빌드 에러, npm ci --legacy-peer-deps

aws amplify로 호스팅을 하려고 하는데 에러가 났다. 

사실 다른 방식으로 호스팅하려다가 잘 안되니까 기다리는 과정에서 amplify는 간단하다는데 해볼까 했던 건데, 역시나 바로 되진 않더라!

 

문제는 

2023-08-16T06:30:37.573Z [WARNING]: npm ERR! Could not resolve dependency: npm ERR! peer react@"> 15.5.0 < 17" from react-twitter-auth@0.0.13 npm ERR! node_modules/react-twitter-auth npm ERR! react-twitter-auth@"^0.0.13" from the root project npm ERR! npm ERR! Conflicting peer dependency: react@16.14.0 npm ERR! node_modules/react npm ERR! peer react@"> 15.5.0 < 17" from react-twitter-auth@0.0.13 npm ERR! node_modules/react-twitter-auth npm ERR! react-twitter-auth@"^0.0.13" from the root project npm ERR! npm ERR! Fix the upstream dependency conflict, or retry npm ERR! this command with --force, or --legacy-peer-deps npm 2023-08-16T06:30:37.573Z [WARNING]: ERR! to accept an incorrect (and potentially broken) dependency resolution. npm ERR! npm ERR! See /root/.npm/eresolve-report.txt for a full report. 2023-08-16T06:30:37.574Z [WARNING]: 2023-08-16T06:30:37.574Z [WARNING]: npm ERR! A complete log of this run can be found in: npm ERR! /root/.npm/_logs/2023-08-16T06_30_33_258Z-debug-0.log 2023-08-16T06:30:37.587Z [ERROR]: !!! Build failed 2023-08-16T06:30:37.587Z [ERROR]: !!! Non-Zero Exit Code detected 2023-08-16T06:30:37.587Z [INFO]: # Starting environment caching... 2023-08-16T06:30:37.588Z [INFO]: # Environment caching completed Terminating logging...

이런식으로 react와 관련 패키지 간 peer dependency 충돌이 났다. 근본적인 원인으로는 프로젝트에서 사용한 리액트 버전은 react@18.2.0인데, 사용한 라이브러리 중 일부가 이 버전의 리액트를 지원하지 않기 때문이다. 

 

리액트 버전을 다운그레이드 하거나, 라이브러리(가 최신 버전을 지원하고 있다면)를 업데이트하는 방법도 있지만, ASAP으로 해결하기 위해 aws 빌드 설정을 수정해주었다. 

aws > amplify > 좌측 탭

빌드 설정에 들어가서 앱 빌드 사양에서 amplify.yml의 script를 수정하면 된다. 

commands: 부분에 --legacy-peer-deps 를 써주면 된다!

version: 1
frontend:
  phases:
    preBuild:
      commands:
     	 # 이 부분을 수정해줬다
        - npm ci --legacy-peer-deps 
    build:
      commands:
        - npm run build
  artifacts:
    # IMPORTANT - Please verify your build output directory
    baseDirectory: /
    files:
      - '**/*'
  cache:
    paths:
      - node_modules/**/*

짠 그럼 빌드 되지롱