npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

better-mitt

v1.0.1

Published

Enhanced tiny (~400b) functional event emitter / pubsub with TypeScript support

Readme

Better Mitt

Enhanced tiny (~400b) functional event emitter / pubsub with TypeScript support.

Features

  • Microscopic: Maintains a small footprint (~400 bytes gzipped)
  • Useful: Wildcard "*" event type listens to all events
  • Familiar: Same API as Node's EventEmitter and original Mitt
  • Functional: Methods don't rely on this
  • TypeScript Ready: First-class TypeScript support with generic types
  • Enhanced API: Adds useful methods like once(), clear(), hasListeners(), and listenerCount()
  • Safer Emission: Protects against modification of handlers during emission

Installation

npm install better-mitt
# or
yarn add better-mitt
# or
pnpm add better-mitt

Usage

ESM (ECMAScript Module)

import mitt from 'better-mitt'

const emitter = mitt()

CommonJS

const mitt = require('better-mitt').default

const emitter = mitt()

CDN

<!-- UMD via unpkg -->
<script src="https://unpkg.com/better-mitt/dist/index.min.js"></script>

<!-- or via jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/better-mitt/dist/index.min.js"></script>

<script>
  // window.mitt is available
  const emitter = mitt()
</script>

Basic Example

import mitt from 'better-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' })

// listen once
emitter.once('bar', e => console.log('bar was called once', e))

// check if has listeners
if (emitter.hasListeners('foo')) {
  console.log('Has foo listeners')
}

// get listener count
console.log(`There are ${emitter.listenerCount('foo')} foo listeners`)

// clearing all events
emitter.clear()

// working with handler references
function onFoo() {}
emitter.on('foo', onFoo)   // listen
emitter.off('foo', onFoo)  // unlisten

TypeScript Usage

This package includes TypeScript definitions. For best type inference, set "strict": true in your tsconfig.json.

import mitt, { Emitter } from 'better-mitt';

// Define your event types
type Events = {
  foo: string;
  bar?: number;
  baz: { name: string };
};

// Create a typed emitter
const emitter = mitt<Events>();

// Event handlers are properly typed
emitter.on('foo', (e) => {
  // 'e' is inferred as string
  console.log(e.toUpperCase());
});

emitter.on('baz', (e) => {
  // 'e' is inferred as { name: string }
  console.log(e.name);
});

// Type checking on emit
emitter.emit('foo', 'hello'); // OK
emitter.emit('foo', 123); // Error: number is not assignable to string

API

mitt()

Creates a mitt event emitter instance.

Returns Emitter

emitter.all

A Map of event names to registered handler functions.

emitter.on(type, handler)

Register an event handler for the given type.

  • type (String|Symbol): Type of event to listen for, or '*' for all events
  • handler (Function): Function to call in response to the event

emitter.off(type, [handler])

Remove an event handler for the given type.

  • type (String|Symbol): Type of event to unregister handler from, or '*'
  • handler (Function) [optional]: Handler function to remove

If handler is omitted, all handlers of the given type are removed.

emitter.emit(type, event)

Invoke all handlers for the given type. If present, '*' handlers are invoked after type-matched handlers.

  • type (String|Symbol): The event type to invoke
  • event (Any): Any value (object is recommended and powerful), passed to each handler

emitter.once(type, handler)

Register an event handler that will be called only once.

  • type (String|Symbol): Type of event to listen for, or '*' for all events
  • handler (Function): Function to call in response to the event

emitter.clear()

Remove all event handlers.

emitter.hasListeners(type)

Check if there are any listeners for a given event type.

  • type (String|Symbol): Type of event to check
  • Returns (Boolean): true if there are listeners for this event type

emitter.listenerCount(type)

Get the number of listeners for a given event type.

  • type (String|Symbol): Type of event to check
  • Returns (Number): Number of listeners for this event type

Browser Support

This package works in all modern browsers and IE11+.

Publishing to npm

If you're forking this package and want to publish your own version, follow these steps:

  1. Create an npm account if you don't already have one
  2. Update the package name, author, and repository information in package.json
  3. Login to npm:
    npm login
  4. Publish the package:
    npm publish

For more information about publishing to npm, see the npm documentation.

License

MIT