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

method-missing

v2.0.0

Published

Catch calls to missing methods on ES6 classes and objects.

Readme

Method Missing

CI npm

Catch calls to methods that do not exist. Extend MethodMissing from your ES6 class (or base class), define __call(name, args), and every call to a missing method is handed to it. Small, with no dependencies.

It also works for static methods and plain objects.

MethodMissing is used in extends-classes, which allows a class to extend several classes.

Installation

$ npm install method-missing

Usage

const MethodMissing = require('method-missing');

class Simple extends MethodMissing {

  __call(name, args) {
    return `The method '${name}' was called with: ${args.join(', ')}`;
  }

}

const simple = new Simple();

simple.nonExistent('Hello!');
// The method 'nonExistent' was called with: Hello!

__call receives the method name and an array of the arguments. Whatever it returns is returned to the caller, and this is the instance, so return this makes missing methods chainable.

Methods, properties and getters that do exist behave as normal.

Static methods

Wrap the class with MethodMissing.static and define a static __call:

class Simple {

  static __call(name, args) {
    return `The method '${name}' was called with: ${args.join(', ')}`;
  }

}

const Wrapped = MethodMissing.static(Simple);

Wrapped.nonExistentStatic(1, 2, 3);
// The method 'nonExistentStatic' was called with: 1, 2, 3

A class can do both: extend MethodMissing for its instances and be wrapped with MethodMissing.static for its static methods.

Objects

Pass a handler function as the second argument:

const object = MethodMissing.static({
  one() {
    return 'hey there';
  }
}, (name, args) => `Sorry, method '${name}' doesn't exist.`);

object.one(); // hey there
object.two(); // Sorry, method 'two' doesn't exist.

Changing the handler name

Pass the name to super, and to MethodMissing.static for static methods:

class Test extends MethodMissing {

  constructor() {
    super('missing');
  }

  missing(name, args) {
    return `The method '${name}' was called with: ${args.join(', ')}`;
  }

  static missing(name, args) {
    return `The static method '${name}' was called with: ${args.join(', ')}`;
  }

}

const Wrapped = MethodMissing.static(Test, 'missing');

What counts as missing

Reading any property that does not exist on the object returns a function that calls your handler, so typeof simple.anything is 'function'. Three kinds of lookup are left alone and return undefined, because the language makes them on every object:

  • then, so instances can be awaited, resolved by promises and returned from async functions.
  • toJSON, so instances can be passed to JSON.stringify.
  • Symbols, such as Symbol.iterator.

Define any of these on your class and they work as usual.

If there is no handler (no __call on the class), missing properties are undefined, the same as on any object.

Upgrading from 1.x

  • The value returned by the handler is now returned to the caller. In 1.x it was discarded.
  • then, toJSON and symbols are no longer handed to the handler. In 1.x this made await instance hang and JSON.stringify(instance) return undefined.
  • A class with no handler no longer throws MethodMissingError when a missing property is read; the property is undefined.
  • Getters now run with the instance, not the unwrapped object, as this.

Tests

$ npm install
$ npm test

License

MIT