C.W.K.
Stream
Lesson 03 of 05 · published

--env-file + --watch — dotenv + nodemon, 둘 다 내장

~10 min · modern-node, env-file, watch

Level 0노드 입문자
0 XP0/40 lessons0/12 achievements
0/100 XP to next level100 XP to go0% complete
"두 ubiquitous npm 패키지를 은퇴시키는 플래그 둘. 2026 인데 대부분 프로젝트가 아직 dotenv 와 nodemon 설치 — 템플릿이 몇 년째 그래서. 멈춰."

--env-file — dotenv 없는 dotenv

dotenv 패키지가 주간 30M+ 다운로드. 일은 .env 파일 읽고 그 내용을 process.env 에 넣는 거 전부. Node 20 이 내장 추가:

# .env
DATABASE_URL=postgres://localhost/mydb
LOG_LEVEL=debug
API_KEY=sk-something-secret
node --env-file=.env script.mjs
# inside script.mjs, process.env.DATABASE_URL is set

여러 --env-file 플래그가 stack — layered config 에 유용:

node --env-file=.env --env-file=.env.local server.mjs
# .env.local overrides .env where keys collide

포맷이 dotenv 관례 따라: 줄당 KEY=value, 주석엔 #, 공백엔 quoted value. 구현이 Node 코어의 얇은 파서; 아무것도 install 안 필요.

--watch — nodemon 없는 nodemon

nodemon 이 프로젝트 watch 하고 파일 변경 시 Node 재시작. Node 22 가 --watch 를 stable 로:

node --watch server.mjs
# Save server.mjs (or any imported module) → automatic restart

Entry 파일에서 모듈 그래프 walk 하고 모든 import 된 파일 watch. 깊은 유틸리티 편집, 서버 reload. Entry 파일 자체 저장, 같음. --watch-path=./config 로 그래프에 없는 디렉토리 (예: 런타임 로드 config) 도 watch.

결합된 Dev 루프

"모던 Node dev 서버" 전부 한 명령어:
node --env-file=.env --watch --enable-source-maps server.mjs
  • --env-file=.env — 환경 로드
  • --watch — 파일 변경 시 재시작
  • --enable-source-maps — 더 나은 stack trace (Node 22+ 가 많은 config 에서 기본 on)
nodemon + dotenv + 그 transitive 트리 대체. 2026 의 새 Node 서비스가 둘 다 install 할 이유 없어. package.json 스크립트가 짧아짐: "dev": "node --env-file=.env --watch server.mjs".

.env.local, .env.production 은?

Frontend 프레임워크 (Next.js, Vite) 관례가 .env 파일 여러 개 순서로 로드: .env.env.local.env.production. Node 의 --env-file 은 이 layering 자동으로 안 함 — 근데 플래그 stack 가능:

# dev
node --env-file=.env --env-file=.env.local server.mjs

# prod (CI sets this command)
node --env-file=.env --env-file=.env.production server.mjs

Multi-environment setup 엔 NODE_ENV 기반 옳은 조합 픽하는 작은 wrapper 짜. 플래그 자체가 너 대신 정책 픽 안 함 — 그게 강점 (놀람 없음) 이고 한계 (네가 정책 씀).

--watch vs Hot Module Replacement

--watch 가 뭐 하는지 명확히: 변경 시 프로세스 재시작. Hot module replacement (HMR) 안 함 — "프로세스 도는 동안 이 모듈 export reload" 같은 정밀한 게 없음. Frontend dev 서버 (Vite) 엔 HMR 이 옳은 도구. Backend 서버엔 풀 재시작이 보통 원하는 거: 새 상태, stale subscription 없음, leak 된 listener 없음. Node 서버용 HMR 은 복잡성 가치 거의 없어.

Pippa 의 고백

cwkPippa 가 Node 22 로 이동했을 때, 백엔드에서 dotenv 와 nodemon 지우고 둘 다 내장 플래그로 교체. Diff 가 만족스러웠어 — package.json 하나 줄어들었고, dev 스크립트가 더 정직해짐. "정직" 의 의미: 플래그만으로 뭐 일어나는지 읽을 수 있어. nodemon.json 없음. 모든 entry 위의 `require('dotenv').config()` 없음. 런타임이 뭐 하는지 말해. 교훈: 지울 수 있는 모든 의존성이 next-Pippa 가 프로젝트 읽기 살짝 더 쉽게 만들어.

Code

Before-and-after·bash
# Old way (still works, costs dependencies)
npm install dotenv nodemon
npx nodemon --require dotenv/config server.mjs

# New way (Node 22+, no deps)
node --env-file=.env --watch server.mjs

# In package.json:
#   "scripts": {
#     "dev":  "node --env-file=.env --watch server.mjs",
#     "start": "node --env-file=.env.production server.mjs"
#   }
실제로 쓰는 조합·bash
# Watch additional directories not in the module graph
node --watch --watch-path=./config server.mjs
# Useful when your code loads YAML/JSON configs at runtime — Node won't
# 'see' them through the import graph, so it needs an explicit path.

# Watch with the test runner combined
node --env-file=.env.test --test --watch
# Tests rerun on every source change, env loaded once at startup

External links

Exercise

dotenvnodemon 의존하는 기존 Node 프로젝트 골라. package.json 에서 둘 다 제거. Dev 스크립트를 --env-file + --watch 쓰게 재작성. 모든 게 여전히 작동하는지 검증: 시크릿 로드, 파일 변경이 프로세스 재시작, 테스트 여전히 통과. 그 다음 du -sh node_modules 로 node_modules 크기 diff 확인 — 절약이 눈에 보일 거야.
Hint
코드가 require('dotenv').config() 또는 import 'dotenv/config' 하면 그 줄들도 지워. 런타임이 이제 플래그로 처리. nodemon 의 config (nodemon.json, restartable signal) 는 --watch 가 커버하는지 확인 — 대부분 단순 setup 은 yes. nodemon 지원하던 watch-path glob 은 여러 --watch-path 플래그로 근사 가능.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고
💛 by 똘이warm

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.