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

weasley

v1.0.0

Published

A tremendously simple dependency injection container for JavaScript

Readme

WeasleyJS

This is a simple JavaScript dependency registry (or service locator) that serves at least two purposes:

  • Being able to easily swap dependencies programmatically;
  • Making everything easily mockable for testing.

Beware that using a service locator may be an anti-pattern (as opposed to proper dependency injection). That being said, feel free to use this package if it suits your needs and you are okay with the possible drawbacks of this approach.

Setup

npm install --save weasley

or

yarn add weasley

Example

Create a weasley.js module:

import Weasley from 'weasley';

const weasley = new Weasley();

// Register the `awesomeDependency` package under the key `my.awesome.dependency`.
// If `awesomeDependency` has a default export, it will be used.
weasley.register('my.awesome.dependency', () => require('awesomeDependency'));

// Register the `boringExport` named export of the `boringDependency` package under
// the key `my.boring.dependency`.
weasley.register('my.boring.dependency', () => require('boringDependency'), 'boringExport');

// Register all exports (instead of `default`) of `awesomeModule` under the key
// `my.awesome.module`.
weasley.register('my.awesome.module', () => require('./awesomeModule.js'), '*');

export default weasley;

Import your weasley.js module from another module (e.g. awesomeModule.js):

import weasley from './weasley.js';

const awesomeDependency = weasley.container.my.awesome.dependency;
const boringDependency = weasley.container.my.boring.dependency;

export function doThings() {
  awesomeDependency.doSomethingAwesome();
  boringDependency.doSomethingBoring();
}

export default {
  isAwesome: true,
};

In a unit test (example using MochaJS and SinonJS):

import { lazyLoad } from 'weasley';
import weasley from './weasley.js';

// Lazy-load the module to be tested
const awesomeModule = lazyLoad(require.resolve('./awesomeModule.js'), '*');

// Create a mock for a dependency of the module
const myAwesomeMock = {
  doSomethingAwesome: sinon.spy(),
};

describe('awesomeModule', function () {
  before(function () {
    // Override dependency with mock
    weasley.register('my.awesome.dependency', () => myAwesomeMock);
  });

  it('should do something awesome') {
    // ...
  }
});

Lazy-loading vs import/require

When using lazyLoad to import a module, it is not actually imported until it is used for the first time. For instance:

const awesomeModule = lazyLoad(require.resolve('./awesomeModule.js'));
// `./awesomeModule.js` has not been imported yet.

awesomeModule.doSomethingAwesome();
// `./awesomeModule.js` has now been imported.

In unit tests, this behavior allows you to mock dependencies used by the module between the call to lazyLoad and the actual testing of the module.

Also, contrary to using import or require, if you lazy-load the same module at multiple places in your code, you will get different copies of the module.

Be aware that lazy-loading uses the Proxy feature from ES6, which cannot be polyfilled. This means that Proxy must be available in the JavaScript environment that runs your unit tests for lazyLoad to work.

Documentation

Complete documentation is available here.

License

MIT