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

require-fu

v1.0.0

Published

Recursive injection-style requires for directories.

Downloads

2,399

Readme

require-fu

Require-fu is a library designed to provide dependency injection style requires recursively for directories.

Introduction

Require-fu is based on the idea of dependency injection requiring. Take an express application as an example. One way you might use an express app is to define an index.js in your routes directory that returns a hash of things to wire into Express. For example:

var routes = require('./routes');

app.get("/about", routes.about);
app.get("/home", routes.home);

This is less than ideal because information about how to access the code that processes the route is separate from the route itself. I, instead, prefer an injection approach. Like so:

require('./routes')(app);

The app parameter is passed in, and the route's index.js file is responsible for wiring everyone up. Or, more likely, routes/index.js just requires a lot of other files that would do that. So, I may define a routes/index.js like so:

module.exports = function(app) {
  require('./about')(app);
  require('./home')(app);
}

Then, my actual route files will look like:

module.exports = function(app) {
  app.get("/about", function(req, res) {
    // do things
  });
}

I like this style a lot, but with vanilla require it means you have to add one require line to index.js for every handler you want to use. So, I wrote require-fu to provide an alternative.

Usage

There are a few prerequisites to this working correctly. Specifically,

  • You must pass the full path to the directory you want to include. (Example below.)
  • All files under that directory must be requirable files. (e.g. no readme's or other documents)

To use require-fu in the example I presented above, trash routes/index.js and do the following in your root-level app.js:

var requireFu = require('require-fu');

requireFu(__dirname + '/routes')(app);

The routes folder will be recursively searched for modules, and they will be required with app injected into them. Moreover, you can inject as many things as you'd like.

var requireFu = require('require-fu');

requireFu(__dirname + '/routes')(app, jobs, db);

About Me

My name is Matt Farmer. I'm a Software Engineer from Atlanta, GA. As of the time of this writing, By day I'm a code bandit involved at Elemica and Anchor Tab. By night, I'm drawing mad scientist like diagrams on my whiteboard at home, thinking of new ways to take over the Internet and brew the perfect cup of tea.

This was inspired in part by Declan de Wet's Pragmatic Guide to Separating Routes in Express, that got me thinking about applying this concept more generally.