id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id
title
category
status
canonical_id
aliases
duplicate_of
source_trust_level
confidence_score
verification_status
tags
raw_sources
last_reinforced
github_commit
tech_stack
wiki-2026-0508-nodejs-global-namespace-augmenta
Node.js Global Namespace Augmentation
10_Wiki/Topics
verified
self
Node.js Global Augmentation
globalThis Augmentation
Node Global Types
none
A
0.9
applied
nodejs
typescript
declaration-merging
global
types
2026-05-10
pending
language
framework
TypeScript
Node.js
Node.js Global Namespace Augmentation
매 한 줄
"매 globalThis 의 typed extension" . Node.js 환경에서 매 global / globalThis 에 property 추가 시 (e.g. dev-mode singleton, env-typed config, monkey-patch), TS declaration merging 으로 매 type-safe access. 매 declare global { ... } + module file (top-level import/export) 매 정답.
매 핵심
매 왜 필요
Dev hot-reload 의 Prisma/Mongoose client singleton 매 module-level 으로는 reload 마다 reconnect.
process.env 의 strict typing.
Vitest/Jest setup 의 global helper.
Polyfill / shim 매 global 추가.
매 globalThis vs global vs window
globalThis : 매 모든 환경 (Node, browser, worker) — 매 prefer.
global : Node.js 만 — 매 deprecated trend.
window : 매 browser only.
매 응용
Prisma / DB client dev-singleton.
Typed process.env (NODE_ENV, DATABASE_URL).
Test setup global mocks.
Monkey-patched fetch / Buffer.
💻 패턴
globalThis singleton (매 Prisma dev)
Typed process.env
Augment global with custom helper
Test setup augmentation (Vitest)
Monkey-patch with type
Validated env (Zod)
매 결정 기준
상황
Approach
Dev singleton
globalThis cache + declare global
Env typing
namespace NodeJS.ProcessEnv (or Zod)
Test mocks
declare global in setup
Runtime validation
Zod / Valibot — 매 declare 만 매 trust X
Module-scoped state
normal module export, not global
기본값 : 매 globalThis + declare global { var ... } (top-level export 로 module 화).
🔗 Graph
🤖 LLM 활용
언제 : dev singleton, typed env, test global mock, runtime polyfill.
언제 X : app state 의 global stash — 매 anti-pattern. Module export 로 한정.
❌ 안티패턴
No export {} : 매 d.ts 가 module 인식 X — 매 global pollute or merge 실패.
global.x = ... without declare : 매 TS error or any.
Production singleton on global : 매 cluster mode worker 별 다른 instance.
Trust env type only : 매 runtime validation 없이 매 typo 매 silent undefined.
Mutate built-ins : 매 Array.prototype 매 augment — library 호환성 폭망.
🧪 검증 / 중복
Verified (TypeScript Handbook — Declaration Merging, Node.js docs globalThis, Prisma docs Best Practices).
신뢰도 A.
🕓 Changelog
날짜
변경
2026-05-08
Phase 1
2026-05-10
Manual cleanup — Prisma singleton, ProcessEnv, Zod env, monkey-patch 패턴