This loadsprocess.env.DB_HOST,process.env.DB_USER, andprocess.env.DB_PASSinto the Node.js environment automatically allowing you to use them inNext.js data fetching methodsandAPI routes.
process.env.DB_HOST, process.env.DB_USER์process.env.DB_PASS๋ฅผ Node.js ํ๊ฒฝ์ผ๋ก ์๋์ผ๋ก ๋ก๋ํ์ฌ Next.js data fetching methods์ API routes์์ ํ๊ฒฝ๋ณ์๋ค์ ์ฌ์ฉํ ์ ์๋ค.
For example, usinggetStaticProps: ์๋ฅผ ๋ค๋ฉด getStaticProps ์ฌ์ฉํ๋ ๊ฒ์ด๋ค:
Note: In order to keep server-only secrets safe, Next.js replacesprocess.env.*with the correct values at build time. This means thatprocess.envis not a standard JavaScript object, so you’re not able to useobject destructuring. Environment variables must be referenced as e.g.process.env.NEXT_PUBLIC_PUBLISHABLE_KEY,notconst { NEXT_PUBLIC_PUBLISHABLE_KEY } = process.env.
*Thedestructuring assignmentsyntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables. *๊ตฌ์กฐ ๋ถํด ํ ๋น๊ตฌ๋ฌธ์ ๋ฐฐ์ด์ด๋ ๊ฐ์ฒด์ ์์ฑ์ ํด์ฒดํ์ฌ ๊ทธ ๊ฐ์ ๊ฐ๋ณ ๋ณ์์ ๋ด์ ์ ์๊ฒ ํ๋ JavaScript ํํ์์ ๋๋ค. MDN Web Docs ์ฐธ์กฐ>>
Note: Next.js will automatically expand variables ($VAR) inside of your .env* files. This allows you to reference other secrets, like so: ๋ ธํธ: Next.js๋ .env*ํ์ผ ์์ ์๋ ๋ณ์($VAR)๋ค์ ์๋์ผ๋ก ํ์ฅํ ๊ฒ์ด๋ค. ์๋ ํ์ฅ์ ํตํด ๋ค๋ฅธ ๋น๋ฐ๋ค์ ์ด์ ๊ฐ์ด ์ฐธ์กฐํ ์ ์๋ค.
If you are trying to use a variable with a$in the actual value, it needs to be escaped like so:\$. ์ค์ ๊ฐ์ $๋ฅผ ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด, \$๊ฐ์ด ์ฒ๋ฆฌํด์ผ ํ๋ค.
For example:
Exposing Environment Variables to the Browser
By default all environment variables loaded through .env.local are only available in the Node.js environment, meaning they won't be exposed to the browser. ๊ธฐ๋ณธ์ผ๋ก .env.local์ ํตํด ๋ก๋๋ ๋ชจ๋ ํ๊ฒฝ๋ณ์๋ Node.js ํ๊ฒฝ์์๋ง ์ฌ์ฉํ ์ ์๋๋ฐ, ์ด ์๋ฏธ๋ ํ๊ฒฝ๋ณ์๊ฐ ๋ธ๋ผ์ฐ์ ์๋ ๋ ธ์ถ๋์ง ์๋๋ค๋ ๊ฒ์ด๋ค.
In order to expose a variable to the browser you have to prefix the variable withNEXT_PUBLIC_. For example: ๋ธ๋ผ์ฐ์ ์ ๋ณ์๋ฅผ ๋ ธ์ถ์ํค๊ธฐ ์ํด์๋ NEXT_PUBLIC_์ด๋ผ๋ ์ ๋์ฌ๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค. ์๋ฅผ ๋ค๋ฉด:
This loadsprocess.env.NEXT_PUBLIC_ANALYTICS_IDinto the Node.js environment automatically, allowing you to use it anywhere in your code. The value will be inlined into JavaScript sent to the browser because of theNEXT_PUBLIC_prefix. This inlining occurs at build time, so your variousNEXT_PUBLIC_envs need to be set when the project is built. ์ด๋ ๊ฒ ํ๋ฉด process.env.NEXT_PUBLIC_ANALYTICS_ID๊ฐ Node.js ํ๊ฒฝ์ ์๋์ผ๋ก ๋ก๋๋๊ณ ์ฝ๋ ์ด๋์์๋ ์ด ํ๊ฒฝ๋ณ์๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ๊ฐ์ NEXT_PUBLIC_ ์ ๋์ฌ๋ก ๋ธ๋ผ์ฐ์ ๋ก ๋ณด๋ด์ง๋ JavaScript์ ์ธ๋ผ์ธ ๋๋ค. ์ด ์ธ๋ผ์ด๋์ ๋น๋ ์๊ฐ์ ์ผ์ด๋จ์ผ๋ก ํ๊ฒฝ๋ณ์๋ค์ ํ๋ก์ ํธ๊ฐ ๋น๋๋ ๋ ์ฌ๋ฌ NEXT_PUBLIC_ ํ๊ฒฝ์ ์ค์ ํด์ผํ๋ค.
Default Environment Variables
In general only one .env.local file is needed. However, sometimes you might want to add some defaults for thedevelopment(next dev) orproduction(next start) environment. ๋ณดํต ํ๋์ .env.local ํ์ผ๋ง ํ์ํ๋ค. ํ์ง๋ง ๋๋๋ก ๊ฐ๋ฐ ํ๊ฒฝ(next dev)์ด๋ ์ด์ ํ๊ฒฝ(next start)์ ์ํ ๋ช๋ช ๊ธฐ๋ณธ ๊ฐ๋ค์ ์ถ๊ฐํ๊ณ ์ถ์ ์ ์๋ค.
Next.js allows you to set defaults in .env (all environments), .env.development (development environment), and .env.production (production environment). Next.js์์๋ .env(๋ชจ๋ ํ๊ฒฝ), .env.development(๊ฐ๋ฐ ํ๊ฒฝ), ๊ทธ๋ฆฌ๊ณ .env.production(์ด์ ํ๊ฒฝ)์ ๊ธฐ๋ณธ๊ฐ๋ค์ ์ค์ ํ ์ ์๋ค.
Note: .env, .env.development, and .env.production files should be included in your repository as they define defaults. .env*.local should be added to .gitignore, as those files are intended to be ignored. .env.local is where secrets can be stored. ๋ ธํธ: .env, .env.development, ๊ทธ๋ฆฌ๊ณ .env.production ํ์ผ๋ค์ ๊ธฐ๋ณธ๊ฐ์ ์ ์ํ ๋ ์ ์ฅ์์ ํฌํจ๋์ด ์์ด์ผํ๋ค. .env*.local์ ๋ฌด์๋๋ ํ์ผ๋ค๋ก .gitignore์ ์ถ๊ฐ๋์ด ์์ด์ผํ๋ค. .env.local์ ๋น๋ฐ์ด ์ ์ฅ๋ ์ ์๋ ๊ณณ์ด๋ค.
Environment Variables on Vercel
When deploying your Next.js application toVercel, Environment Variables can be configuredin the Project Settings. Vercel์ Next.js ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ฐฐํฌํ ๋์ ํ๊ฒฝ๋ณ์๋ ํ๋ก์ ํธ ์ค์ ์์ ๊ตฌ์ฑ๋ ์ ์๋ค.
All types of Environment Variables should be configured there. Even Environment Variables used in Development – which can bedownloaded onto your local deviceafterwards. ๋ชจ๋ ํ์ ์ ํ๊ฒฝ๋ณ์๋ ํ๋ก์ ํธ ์ค์ ์์ ๊ตฌ์ฑ๋์ด์ผ ํ๋ค. ๊ฐ๋ฐ ํ๊ฒฝ์์ ์ฌ์ฉ๋ ํ๊ฒฝ๋ณ์๋ ์ดํ์ ๋ก์ปฌ ๊ธฐ๊ธฐ์ ๋ค์ด๋ก๋ํ ์ ์๋ค.
If you've configuredDevelopment Environment Variablesyou can pull them into a .env.local for usage on your local machine using the following command: ๊ฐ๋ฐ ํ๊ฒฝ๋ณ์๋ฅผ ๊ตฌ์ฑํ ๊ฒฝ์ฐ ๋ก์ปฌ ๋จธ์ ์์ ์ฌ์ฉํ ์ ์๋๋ก ์๋์ ๋ช ๋ น์ด๋ฅผ ํตํด .env.local๋ก ๊ฐ์ ธ์ฌ ์ ์๋ค.
When using the Vercel CLI to deploy make sure you add a .vercelignorethat includes files that should not be uploaded, generally these are the same files included in .gitignore. Vercel CLI์ ์ฌ์ฉํ์ฌ ๋ฐฐํฌํ ๋ ์ ๋ก๋๋์๋ ์ ๋๋ ํ์ผ๋ค์ ํฌํจํ๋ .vercelignore๋ฅผ ์ถ๊ฐํ์๋์ง ํ์ธํ๋ผ. ๋๊ฐ .vercelignore์ ํฌํจ๋์ด์ผ ํ๋ ํ์ผ๋ค์ .gitignore์ ํ์ผ๋ค๊ณผ ๊ฐ๋ค.
Test Environment Variables
Apart fromdevelopmentandproductionenvironments, there is a 3rd option available:test. In the same way you can set defaults for development or production environments, you can do the same with .env.test file for testing environment (though this one is not so common as the previous two). ๊ฐ๋ฐ๊ณผ ์ด์ ํ๊ฒฝ ์ธ์, ์ธ ๋ฒ์งธ ์ต์ ๋ ์ฌ์ฉํ ์ ์๋ค. ๊ทธ ์ต์ ์ ํ ์คํธ์ด๋ค. ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ๊ฐ๋ฐ ํน์ ์ด์ ํ๊ฒฝ์ ์ํ ๊ธฐ๋ณธ๊ฐ์ ์ค์ ํ ์ ์๊ณ , ํ ์คํธ ํ๊ฒฝ์ ์ํด์ .env.test ํ์ผ์๋ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ์ค์ ํ ์ ์๋ค. (์ด ๋ฐฉ๋ฒ์ ์ด์ ๋ ๋ฐฉ๋ฒ์ฒ๋ผ ๋ณดํธ์ ์ด์ง ์๋ค.)
This one is useful when running tests with tools likejestorcypresswhere you need to set specific environment vars only for testing purposes. Test default values will be loaded ifNODE_ENVis set totest, though you usually don't need to do this manually as testing tools will address it for you. ์ด๊ฒ์ ํ ์คํธ ๋ชฉ์ ์ผ๋ก๋ง ํน์ ํ๊ฒฝ๋ณ์๋ฅผ ์ค์ ํด์ผ ํ๋ jest ๋๋ cypress์ ๊ฐ์ ํ ์คํ ํด์ ์คํํ ๋ ์ ์ฉํ๋ค. ํ ์คํธ ๊ธฐ๋ณธ๊ฐ์ NODE_ENV๊ฐ ํ ์คํธ๋ก ์ค์ ๋์ด ์์ผ๋ฉด ํ ์คํธ ๊ธฐ๋ณธ ๊ฐ์ด ๋ก๋๋ ๊ฒ์ธ๋ฐ ๋๊ฐ ํ ์คํ ํด์ด ์๋์ผ๋ก ์ฒ๋ฆฌํด์ค ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์๋์ผ๋ก ์ค์ ํ ํ์๋ ์๋ค.
There is a small difference betweentestenvironment, and bothdevelopmentandproductionthat you need to bear in mind: .env.local won't be loaded, as you expect tests to produce the same results for everyone. This way every test execution will use same env defaults across different executions by ignoring your .env.local (which is intended to override the default set). ๊ธฐ์ตํด๋ ํ์๊ฐ ์๋ ํ ์คํธ ํ๊ฒฝ๊ณผ ๊ฐ๋ฐ, ์ด์ ํ๊ฒฝ์ ์์ ์ฐจ์ด์ ์ด ์๋ค: .env.local์ ๋ชจ๋์๊ฒ ํ ์คํธ ๊ฒฐ๊ณผ๊ฐ ๋์ผํ ๊ฒ์ผ๋ก ์์๋์ด ๋ก๋๋์ง ์์ ๊ฒ์ด๋ค. ์ด๋ฌํ ๋ฐฉ๋ฒ์ผ๋ก ๋ชจ๋ ํ ์คํธ ์คํ์ .env.local(๊ธฐ๋ณธ ์ค์ ์ ์ค๋ฒ๋ผ์ด๋ ํ๊ธฐ ์ํ ๊ฒ)์ ๋ฌด์ํ์ฌ ์ฌ๋ฌ ์คํ์์ ๋์ผํ env ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํ๋ค.
Note: similar to Default Environment Variables, .env.test file should be included in your repository, but .env.test.local shouldn't, as .env*.local are intended to be ignored through .gitignore. ๋ ธํธ: ๊ธฐ๋ณธ ํ๊ฒฝ ๋ณ์์ ๋น์ทํ๊ฒ, .env.test ํ์ผ์ ์ ์ฅ์์ ํฌํจ๋์ด์ผ ํ๋ค, ํ์ง๋ง .env.test.local์ .env*.local์ด .gitignore๋ฅผ ํตํด ๋ฌด์๋๋ ๊ฒ์ฒ๋ผ ์ ์ฅ์์ ํฌํจ๋์๋ ์ ๋๋ค.
While running unit tests you can make sure to load your environment variables the same way Next.js does by leveraging theloadEnvConfigfunction from the@next/envpackage. ๋จ์ ํ ์คํธ๋ฅผ ์คํํ๋ ๋์ @next/env ํจํค์ง์ loadEnvConfig์ ํ์ฉํ์ฌ Next.js์ ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก ํ๊ฒฝ๋ณ์๋ฅผ ๋ก๋ํ ์ ์๋ค.
// The below can be used in a Jest global setup file or similar for your testing set-up
import { loadEnvConfig } from '@next/env'
export default async () => {
const projectDir = process.cwd()
loadEnvConfig(projectDir)
}