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

requirefix

v0.0.9

Published

A lightweight extension for Node.js's require() function. This extension allows you to load a module with its automatically pre- and/or post fixed name.

Downloads

19

Readme

Requirefix

What's Requirefix?

Requirefix is a lightweight extension for Node.js's require() function. This extension allows you to load a module with its automatically postfixed name.

The Problem

If you're using js-to-js transpilers or pre-processors like Babel or Traceur for your Node.js source code, you'll early face the problem you have two versions of the same file: an original and a compiled with different names. server.js and server-compiled.js for example. It not seems like a good design decision to insert -compiled into the module names in your require() calls.

The Solution

Requirefix extends Node's built-in require() function with an extra behaviour. You can specify one or more prefix and postfix for the module paths you try to require. This way you can simply include modules without worrying about the pre-processed and original file version names.

Usage

Add it to your application's main module to override module.require() globally:


require('requirefix').postfix = '-compiled' // override module.require() and set the desired postfix in one line

// here we're using requirefix() instead of the original module.require()
// try to load './myCompiledModule-compiled.js' and './myCompiledModule.js'
var myCompiledModule = require('./myCompiledModule')

If you don't want to override module.require() globally, but you want to use it as a drop in replacement, that's not a problem:


var rfx = require('requirefix')

rfx.dropIn  = true // tell requirefix to restore global.require() and module.require() to the original, built-in require() function 
rfx.postfix = '-compiled' // set the desired postfix

require = rfx.bind(module) // override module.require() only for this module

// here we're using requirefix() instead of the original module.require()
// try to load './myCompiledModule-compiled.js' and './myCompiledModule.js'
var myCompiledModule = require('./myCompiledModule')

You can specify multiple postfixes at the same time:


require('requirefix').postfix = [ '-compiled', '.min' ]

// try to load './myCompiledModule-compiled.js', './myCompiledModule.min.js' and './myCompiledModule.js'
var myCompiledModule = require('./myCompiledModule') 

Prefixes are also supported:


var rfx = require('requirefix')

rfx.prefix  = '_' // auto-prefix with an underscore, because we mark private modules with it, for example
//rfx.prefix = [ '_', '__' ] // multiple prefixes are also supported
rfx.postfix = '-compiled'
//rfx.postfix = [ '-compiled', '.min' ] // or even multiple pre- and post fixes at the same time
 
// try to load:
// * './lib/_myCompiledModule-compiled'
// * './lib/_myCompiledModule'
// * './lib/myCompiledModule-compiled'
// * './lib/myCompiledModule'
var myCompiledModule = require('./lib/myCompiledModule')

For more information see the examples.

Installation

With npm:

npm install --save requirefix

With git:

git clone git://github.com/schwarzkopfb/requirefix.git
cd requirefix
npm test

Will Requrefix break my dependencies?

Requrefix leaves native and locally installed modules alone. If you use Requrefix to load one of Node's built-in modules or a package from the node_modules folder, Requirefix won't mess with it.

Performance

Requirefix adds minimal and hardly measurable overhead to the module-loading process.

License

MIT license.