mitt and nanoevents are ultra-small, dependency-free event emitter libraries designed to replace the heavy EventEmitter found in Node.js or the verbose native EventTarget in browsers. They provide a simple API for publishing and subscribing to events, making them ideal for state management, component communication, and decoupling logic in modern applications. While both solve the same core problem with a tiny footprint, mitt focuses on a familiar Map-like API with wildcard support, whereas nanoevents prioritizes absolute minimalism and tree-shakable ES module design.
In modern frontend development, we often need a way for different parts of our application to talk to each other without being tightly coupled. While browsers provide the native EventTarget API and Node.js offers EventEmitter, both can be too heavy or verbose for simple tasks like managing global UI state or tracking analytics. This is where mitt and nanoevents step in. They are both tiny, zero-dependency libraries that solve this problem, but they take slightly different approaches to API design and feature sets.
The most immediate difference you'll notice is how you create and use the emitter. mitt returns an object that looks and feels like a Map, while nanoevents returns a simple object with an on and emit method.
mitt uses a Map-like interface. You create an emitter and immediately have access to methods like on, off, and emit. It also exposes the internal storage if you need it, though direct manipulation is rarely required.
import mitt from 'mitt';
const emitter = mitt();
// Subscribe to an event
emitter.on('user:login', (user) => {
console.log(`Welcome, ${user.name}`);
});
// Emit an event
emitter.emit('user:login', { name: 'Alice' });
// Unsubscribe (requires the exact same function reference)
const handler = (data) => console.log(data);
emitter.on('data:update', handler);
emitter.off('data:update', handler);
nanoevents is even more stripped down. It exports a class (or a function depending on how you import it) that you instantiate. The API is minimal: just on to listen and emit to trigger.
import { NanoEvents } from 'nanoevents';
const emitter = new NanoEvents();
// Subscribe to an event
emitter.on('user:login', (user) => {
console.log(`Welcome, ${user.name}`);
});
// Emit an event
emitter.emit('user:login', { name: 'Alice' });
// Unsubscribe
const handler = (data) => console.log(data);
emitter.on('data:update', handler);
emitter.off('data:update', handler);
One of the biggest practical differences is support for wildcard events. If you are building a complex state store or an analytics system, you often want to listen to a category of events rather than listing every single one.
mitt supports wildcards out of the box. You can use the * character to listen to every event, or specific patterns if you structure your event names carefully (though it primarily supports a global * listener for all events).
import mitt from 'mitt';
const emitter = mitt();
// Listen to EVERY event
emitter.on('*', (type, data) => {
console.log(`Event fired: ${type}`, data);
});
emitter.emit('user:login', { id: 1 }); // Logs:
Choose mitt if you need a robust, drop-in replacement for standard event emitters that supports wildcard listeners (e.g., listening to all 'user:*' events). It is the better choice for complex state management scenarios where you might need to remove specific handlers by reference or clear all listeners for a specific event type efficiently. Its API feels more like a traditional Map, making it intuitive for developers coming from Vue or other framework backgrounds.
Choose nanoevents if your primary constraint is bundle size and you only need basic publish/subscribe functionality without wildcards. It is perfect for simple communication channels, like notifying a UI of a background task completion, where the overhead of extra features is unnecessary. Its design encourages creating separate emitter instances for different concerns, which aligns well with modular, tree-shaken architectures.
Tiny 200b functional event emitter / pubsub.
"*" event type listens to all eventsthisMitt was made for the browser, but works in any JavaScript runtime. It has no dependencies and supports IE9+.
This project uses node and npm. Go check them out if you don't have them locally installed.
$ npm install --save mitt
Then with a module bundler like rollup or webpack, use as you would anything else:
// using ES6 modules
import mitt from 'mitt'
// using CommonJS modules
var mitt = require('mitt')
The UMD build is also available on unpkg:
<script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>
You can find the library on window.mitt.
import mitt from 'mitt'
const emitter = mitt()
// listen to an event
emitter.on('foo', e => console.log('foo', e) )
// listen to all events
emitter.on('*', (type, e) => console.log(type, e) )
// fire an event
emitter.emit('foo', { a: 'b' })
// clearing all events
emitter.all.clear()
// working with handler references:
function onFoo() {}
emitter.on('foo', onFoo) // listen
emitter.off('foo', onFoo) // unlisten
Set "strict": true in your tsconfig.json to get improved type inference for mitt instance methods.
import mitt from 'mitt';
type Events = {
foo: string;
bar?: number;
};
const emitter = mitt<Events>(); // inferred as Emitter<Events>
emitter.on('foo', (e) => {}); // 'e' has inferred type 'string'
emitter.emit('foo', 42); // Error: Argument of type 'number' is not assignable to parameter of type 'string'. (2345)
Alternatively, you can use the provided Emitter type:
import mitt, { Emitter } from 'mitt';
type Events = {
foo: string;
bar?: number;
};
const emitter: Emitter<Events> = mitt<Events>();
Mitt: Tiny (~200b) functional event emitter / pubsub.
Returns Mitt
A Map of event names to registered handler functions.
Register an event handler for the given type.
type (string | symbol) Type of event to listen for, or '*' for all eventshandler Function Function to call in response to given eventRemove an event handler for the given type.
If handler is omitted, all handlers of the given type are removed.
type (string | symbol) Type of event to unregister handler from, or '*'handler Function? Handler function to removeInvoke all handlers for the given type.
If present, '*' handlers are invoked after type-matched handlers.
Note: Manually firing '*' handlers is not supported.
type (string | symbol) The event type to invokeevt Any? Any value (object is recommended and powerful), passed to each handlerFirst off, thanks for taking the time to contribute! Now, take a moment to be sure your contributions make sense to everyone else.
Found a problem? Want a new feature? First of all see if your issue or idea has already been reported. If don't, just open a new clear and descriptive issue.
Pull requests are the greatest contributions, so be sure they are focused in scope, and do avoid unrelated commits.
git clone https://github.com/<your-username>/mittcd mittgit checkout -b my-new-featurenpm installgit commit -am 'Add some feature'git push origin my-new-feature