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 🙏

© 2024 – Pkg Stats / Ryan Hefner

knockknock

v0.3.0

Published

Who's there? The modules that just called your code

Downloads

30

Readme

888    d8P                             888      888    d8P                             888
888   d8P                              888      888   d8P                              888
888  d8P                               888      888  d8P                               888
888d88K     88888b.   .d88b.   .d8888b 888  888 888d88K     88888b.   .d88b.   .d8888b 888  888
8888888b    888 "88b d88""88b d88P"    888 .88P 8888888b    888 "88b d88""88b d88P"    888 .88P
888  Y88b   888  888 888  888 888      888888K  888  Y88b   888  888 888  888 888      888888K
888   Y88b  888  888 Y88..88P Y88b.    888 "88b 888   Y88b  888  888 Y88..88P Y88b.    888 "88b
888    Y88b 888  888  "Y88P"   "Y8888P 888  888 888    Y88b 888  888  "Y88P"   "Y8888P 888  888

Who's there?
The modules that just called your code.

KnockKnock provides information about the files, functions, and packages that were responsible for calling your module.

Build Coverage Dependencies Dev Dependencies License Release

Install

$ npm install --save knockknock

You'll need to have at least Node.js 4 or newer.

API

knockknock([options])

Finds all of the available information about the callers asynchronously, returning a Promise to retrieve them. The information for each caller is provided in a format similar to below:

{
  // The column number within the file responsible for calling your module.
  column: 10,
  // The file that called your module
  file: '/path/to/my-example-package/node_modules/example-server/src/start.js',
  // The line number within the file responsible for calling your module
  line: 123,
  // The name of the function within the file responsible for calling your module (or "<anonymous>" where appropriate)
  name: 'startServer',
  // The information for the package containing the file or null if none could be found
  package: {
    // The directory of the package
    directory: '/path/to/my-example-package/node_modules/example-server',
    // The file path of the "main" file for the package or null if it has none
    main: '/path/to/my-example-package/node_modules/example-server/server.js',
    // The name of the package
    name: 'example-server',
    // The version of the package
    version: '3.2.1'
  }
}

The options parameter is entirely optional and supports the following:

| Option | Description | Default Value | | ---------------- | ----------------------------------------------------------------------------------------------------------------------------------------- | ------------- | | excludes | The name(s) of packages whose calls should be ignored. Internal calls from KnockKnock and Node.js are always ignored. | [] | | filterFiles | A function called to filter files based on their path. Only called for files whose containing package (if any) is also included. | N/A | | filterPackages | A function called to filter files based on the package to which they belong (if any). Only called if package is not listed in excludes. | N/A | | limit | The maximum number of callers to be included in the results. No limit is applied when null. | null | | offset | The number of frames from call stack to be skipped initially. | 0 |

In most cases, you may want to at least exclude your own package so that your own package-internal calls are ignored via excludes or filterPackages.

const whoIsThere = require('knockknock');

module.exports = function() {
  return whoIsThere({ excludes: 'my-example-package' })
    .then((callers) => {
      if (callers.length > 0) {
        console.log(`Called from ${callers.length} modules`);

        // ...
      } else {
        console.log('Called from unknown module');

        // ...
      }
    });
};

The limit option works great if you only want to know about the last caller:

const whoIsThere = require('knockknock');

module.exports = function() {
  return whoIsThere({ excludes: 'my-example-package', limit: 1 })
    .then((callers) => {
      if (callers.length === 1) {
        console.log(`Called from module "${callers[0].file}" in package "${callers[0].package ? callers[0].package.name : '<unknown>'}"`);

        // ...
      } else {
        console.log('Called from unknown module');

        // ...
      }
    });
};

knockknock.sync([options])

A synchronous alternative to knockknock([options]).

const whoIsThere = require('knockknock');

module.exports = function() {
  const callers = whoIsThere.sync({ excludes: 'my-example-package' });

  if (callers.length > 0) {
    console.log(`Called from ${callers.length} modules`);

    // ...
  } else {
    console.log('Called from unknown module');

    // ...
  }
};

knockknock.version

The current version of KnockKnock.

const whoIsThere = require('knockknock');

whoIsThere.version;
=> "0.3.0"

Bugs

If you have any problems with KnockKnock or would like to see changes currently in development you can do so here.

Contributors

If you want to contribute, you're a legend! Information on how you can do so can be found in CONTRIBUTING.md. We want your suggestions and pull requests!

A list of KnockKnock contributors can be found in AUTHORS.md.

License

See LICENSE.md for more information on our MIT license.

Copyright !ninja