| 선언 시점 | 접근성: CSR | 접근성: SSR | 접근성: Node.js | ||
|---|---|---|---|---|---|
| .env | buildtime / runtime | ✅ | process.env cannot be destructured or accessed with dynamic properties | ||
| prefix: NEXT_PUBLIC inside .env | buildtime | ✅ | ✅ | process.env cannot be destructured or accessed with dynamic properties | |
| next.config.js 내부 env 설정 | buildtime | ✅ | ✅ | process.env cannot be destructured or accessed with dynamic properties | |
| publicRuntimeConfig | runtime | ✅ | ✅ | SSR 페이지에서만 사용 가능 | |
| serverRuntimeConfig | runtime | ✅ | |||
| process.env | runtime | ✅ |
next.config.js 에서 설정을 해주거나 .env 파일 안에서 NEXT_PUBLIC_[ENV] prefix 로 환경변수 선언method 1: next.config.js :
module.export = {
env: {
MY_ENV: proces.env.MY_ENV
}
}
method 1: next.config.js :
NEXT_PUBLIC_MY_ENV:myEnvValue
method 1: publicRuntimeConfig
module.export = {
publicRuntimeConfig: {
MY_ENV: proces.env.MY_ENV
}
}
method 1: serverRuntimeConfig
module.export = {
serverRuntimeConfig: {
MY_ENV: proces.env.MY_ENV
}
}
import getConfig from 'next/config'
const { publicRuntimeConfig, serverRuntimeConfig} = getConfig()
const publicRuntimeEnv = publicRuntimeConfig.MY_ENV
const serverRuntimeEnv = serverRuntimeConfig.MY_ENV