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

requirefire

v0.4.1

Published

A userland re-implementation of `require` for node.js. Useful in situations where you need a separate require cache and loader that can be intricately managed in tests.

Downloads

3,266

Readme

requirefire

A userland re-implementation of require for node.js. Useful in situations where you need a separate require cache and loader that can be intricately managed in tests.

Why

Sometimes you just have no other option -- and really, thats how this module should be used, as a backup to the backup plan. If you can, you should use plain old require. It supports many more things like require extensions, and is way more likely to just work for you.

That said, sometimes it is useful to have a cleanroom implementation of require that you know no other systems have messed with. Testing frameworks like jest mess with require without much opportunity to change how, so if you need to get around it, you can use requirefire for whatever you need finer control over the semantics of. Godspeed on your journey fellow modulonaut.

Installation

npm install requirefire

or

pnpm add requirefire

Usage

The requirefire module exports a factory function which must be called to create a new require function. So, import the createRequirefire function from the module, and then invoke it to create a new version of require.

import createRequirefire from "requirefire"

// create a requirefire instance
const requirefire = createRequirefire()
// require a module
const someModule = requirefire("./someModule")

// requiring the module a second time will return the same instance from a cache, same as normal `require`
someModule == requirefire("./someModule") // true

// requiring a module with requirefire will return a different instance of the same module than normal `require`
someModule == require("./someModule") // false

// the requirefire instance has a .cache property that can be mutated
delete requirefire.cache['path/to/someModule']

// once the cache is cleared, requirefire will re-execute the module from disk when required again
const newSomeModule = requirefire("./someModule")
someModule == newSomeModule // false

// multiple requirefire instances can be created, and each has an independent require cache
const otherRequirefire = createRequirefire()
requirefire("./someModule") == otherRequirefire("./someModule") // false

See Also

  • rewire. requirefire is different in that it overrides transitive requires, caches modules for the same instance, can have multiple instances, and doesn't support monkeypatching
  • proxyquire. requirefire is different in that it can have multiple instances, doesn't support monkeypatching, but doesn't need any explicit stubs for required modules

Credits

Most of this code is from the rewire module, but adjusted to work against a stateful instance with a require cache, and less oriented at monkeypatching. Thanks to Johannes for his hard work!