warn-once

Print a console warning exactly once during development

Library
npm
v0.1.1
9stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
19/100Needs Attention
Development Activity0
Maintenance0
Community16
Maturity60
Momentum0

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
70/100Good
Architecture62
Code Quality66
Innovation55
Learning Curve96

warn-once is a tiny, zero-dependency JavaScript utility that prints a warning message to the console exactly once, no matter how many times it is called with the same message. It is designed for development-time notices such as deprecation warnings or reminders about missing setup, so noisy logs stay readable.

The warnOnce function takes a condition followed by one or more message arguments; it only emits when the condition is truthy and only in non-production environments (when NODE_ENV is not set to production), making it safe to leave in library and application code without cluttering production output.

What You Get

  • A single warnOnce(condition, …messages) function
  • Automatic de-duplication so each unique message prints only once
  • Development-only behavior gated on NODE_ENV not being production
  • Support for multiple message arguments passed straight to console.warn
  • TypeScript type definitions and zero runtime dependencies

Common Use Cases

  • Emitting deprecation warnings from a library without spamming the console
  • Reminding developers about missing or incorrect setup once per session
  • Guarding warnings behind a runtime condition
  • Keeping development logs clean while still surfacing important notices

Under The Hood

Architecture - The entire implementation is a single function in index.js. It holds a module-level Set of already-printed message keys, computes a key by joining the message arguments, and calls console.warn only when the DEV flag is set, the condition is truthy, and the key has not been seen before.

Tech Stack - Plain CommonJS JavaScript with no dependencies, shipping an index.d.ts for TypeScript consumers. The development check reads process.env.NODE_ENV once at module load.

Code Quality - The code is minimal and easy to audit - a dozen lines with a clear guard clause and a Set-based dedupe. There is no test suite in the repository, which is unsurprising given the trivial surface area, though it means correctness rests on the simplicity of the logic.

API Design - The API is about as small as it gets: one default-exported function whose signature (condition first, then variadic messages) mirrors a conditional console.warn. This makes it instantly familiar and trivial to drop into existing warning call sites.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search