mitt
Tiny functional event emitter for JavaScript — 200 bytes, zero dependencies, and works everywhere.
mitt
General JSTiny functional event emitter for JavaScript — 200 bytes, zero dependencies, and works everywhere.
Fit
Bundle (gzip)
282 B
488 B raw
Install size includes transitive dependencies. Bundle size is gzipped browser payload.
Freshness
The Problem
Node's EventEmitter is Node-only. Browser-native EventTarget is verbose. Building a simple publish-subscribe bus for cross-component communication or module decoupling shouldn't require a heavy library.
What It Does
Mitt provides on, off, and emit methods in 200 bytes. It's typed with generics so TypeScript knows which events exist and what their payload types are. Works in browsers, Node.js, Deno, and Web Workers without any polyfills.
Installation
npm install mittUsage Example
import mitt from 'mitt';
type Events = {
'user:login': { userId: string; email: string };
'cart:update': { itemCount: number };
resize: undefined;
};
const emitter = mitt<Events>();
// Listen
emitter.on('user:login', ({ userId, email }) => {
console.log(`User ${email} logged in`);
});
// Emit
emitter.emit('user:login', { userId: '123', email: 'alice@example.com' });
// Unsubscribe
const handler = () => {};
emitter.on('resize', handler);
emitter.off('resize', handler);Related packages
Unified TypeScript SDK for building AI-powered apps — switch between 20+ LLM providers without rewriting code.
animejsGeneral JSLightweight JavaScript animation library with a tiny footprint and a powerful, expressive API.
AOSGeneral JSAn easy-to-use library that makes elements fade, slide, or zoom into view as the user scrolls down your page.
archiverGeneral JSStreaming interface for generating ZIP and TAR archives in Node.js — create compressed archives programmatically from files, directories, and buffers.
@formkit/auto-animateGeneral JS> Add smooth transitions to any element with a single line — zero-config animations for adds, removes, and moves.