Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- rds utf8mb4 변경
- fcm push
- vscode소스제어 무한로딩
- 오블완
- 맥북파이썬여러버전
- {'error': {'errorcode': '200'
- fcm 멀티푸시
- codebuild cliend_error
- 파이썬 설치경로 변경
- fcm multicast
- 'details': 'invalid authentication information.'}}
- vscode시계아이콘
- ncp 401 authentication failed
- 티스토리챌린지
- codepipeline 오류
- command did not exit successfully docker-compose -f docker-compose.yml build exit status 1
- 파이썬버전여러개
- 파이썬 경로 수정
- rds character_set 변경
- ncp api 응답오류
- vscode 소스제어
- rds 파라미터그룹
- 파이썬여러버전변경
- 네이버클라우드 authentication failed
- ncp authentication failed
- vscode무한로딩
- codebuild no matching artifact paths found
- 'message': 'authentication failed'
- 파이썬버전변경
- route53 서브도메인
Archives
- Today
- Total
All that I've dreamed of
React 환경변수 개발/배포 env파일 분리 (+typescript) 본문
반응형
환경변수를 .env.development / .env.production 으로 나누면 React는 CRA 명령어에 따라 적용되는 .env파일이 달라진다
(적용되는 파일의 우선순위가 달라지는 것)
npm run start => .env.development 적용
npm run build => .env.production 적용
npm run test => .env.test 적용
cross-env 모듈 설치
$ npm i cross-env
환경변수 적용 전에는 하드코딩된 코드를 사용해서
로컬에서 실행하거나 빌드를 할 때는 매번 바꿔줘야 했다
const instance = axios.create({
// URL: "http://127.0.0.1:8000/api/v1/", // 개발환경
URL: "https://myproject.com/api/v1/", // 배포환경
withCredentials: true,
})
.env.development 파일과 .env.production 파일을 생성한 뒤 환경 변수를 각각 적용해주면
npm run start 시에는 http://127.0.0.1:8000/api/v1/ 가
npm run build 시에는 http://myproject.com/api/v1/ 이 들어가게 된다
const instance = axios.create({
URL: process.env.REACT_APP_HOST,
withCredentials: true, //세션id
})
# .env.development
REACT_APP_HOST=http://127.0.0.1:8000/api/v1/
# .env.production
REACT_APP_HOST=https://myproject.com/api/v1/
'REACT_APP_변수이름' 과 같이 REACT_APP_을 꼭 붙여주어야 하고
코드 안에서 사용할 때는 process.env.REACT_APP_변수이름 형식으로 쓰면된다
+) TypeScript Error가 발생하는 경우
REACT_APP_ENV 가 undefined 라는 내용의 에러...
변수 타입을 몰라서 typescript가 뭐라 한다면
react-app-env.d.ts 파일에 해당 변수의 타입을 선언해 주면 된다.
# react-app-env.d.ts
/// <reference types="react-scripts" />
declare namespace NodeJS {
interface ProcessEnv {
REACT_APP_HOST: string;
}
}
이러면 에러도 사라지고 자동완성도 쓸 수 있다.
---
.env파일 추가하고 설정 다 해줬으면 ⭐서버 재시작⭐
'ETC' 카테고리의 다른 글
S3 Access Denied 오류 (get_object, delete_object 등) (1) | 2023.03.29 |
---|---|
[Github Actions] React + S3 + CloudFront 자동배포 (+ env 환경변수) (0) | 2023.01.08 |
서브도메인 설정 (Route53, 가비아, cloudfront) (0) | 2023.01.05 |
React / TypeScript Property 'id' does not exist on type 'EventTarget' in TS 오류 (0) | 2022.12.21 |
AWS Cloud9에서 Lightsail (ec2) 인스턴스 열기 (0) | 2022.07.08 |
Comments