[electron] dotenv를 사용하여 eletron에서 .env 설정 방법

조용진·2023년 3월 11일
0

electron

목록 보기
1/1

Version

electron:'^20.0.2'
dotenv:'^16.0.3'
electron-builder"^23.3.3"

문제 원인

폴더 root 최상단에 .env를 생성해서 KEY값을 입력해도 electron node와 BrowserWindow에선 process.env.API_KEY가 undefined로 나타난다.

dotenv 설치

yarn add dotenv

main process에 dotenv 적용

main.ts

import "dotenv/config"

적용후

정상적으로 작동한다.

하지만 BrowserWindow에선 process.env.API_KEY를 호출했더니 process is not defined 오류가 발생했다.

해결방법

main.ts

const mainWindow = new BrowserWindow({
webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
    },
})

webPreferences옵션에 nodeIntegration 는 ture, contextIsolation는 false를 해주면 해결된다.

번외 contextBridge API can only be used when contextIsolation is enabled

contextIsolation 설정을 끄면 아래에 오류가 생긴다.

해결 방법

main process에서 contextBridge 함수 호출을 전부 지워주면 해결된다.

profile
Frontend Developer/공부 내용 정리하기/개발중 오류 기록하기/반가워요:)

0개의 댓글