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-extended

v1.0.2

Published

Extends require with some awesome features

Downloads

12

Readme

Require Extended

Extended require with awesome features!

Build Status Coverage Status npm version


This package lets you modify how require should work. It adds some cool features like requiring from projects root path or redirecting all fs requires to your custom custom-fs.js module.

Installation

  • npm
npm install require-extended
  • yarn
yarn add require-extended

Usage

Import require extended in your entry file.

const requireExtended = require('require-extended')();

Features

Root Path

You can use require('~/file-from-root.js') at anywhere of your project.

If you want to disable this feature or change the prefix you can pass options at your entry file where you first required require-extended.

const requireExtended = require('require-extended')({
    resolveRoot: {
        enabled:true,
        prefix: '?'
    }
});

By default root path is found using the package app-root-path.

If you want to set it to a custom path you can always use setRoot. You can pass the absolute path or relative path. Relative paths will be resolved from the caller file.

const requireExtended = require('require-extended')();
requireExtended.setRoot(myCustomRootPath);

Bindings

You can return custom variables for require statements using .binding() feature. Bindings define what NodeJs should load for require.

const requireExtended = require('require-extended')();
const fsBinding = requireExtended.binding('fs', {
    readFileSync: () => {
        //custom implementation
    }
});

In this example for any file requires fs module they will receive a custom object. You can use any type of variable as a binding.

You can remove the binding by using .restore().

fsBinding.restore();

Now, all require calls to fs will receive the real fs module.

Binding matchers can be string, regex or a function that returns boolean

const requireExtended = require('require-extended')();
const jsonBinding = requireExtended.binding('./folder/language.json', {langugues: []});

All files that requires ./folder/language.json will now receive the custom json data.


  • Bindings are resolved during _load state where NodeJs checks module caches.
  • Bindings are before mimics. If bindings are not matched then mimics will be working.

Mimics

Mimics are different than bindings, they change how NodeJs finds the file that should be loaded. Mimics are helping NodeJs to navigate between right files.

const requireExtended = require('require-extended')();
const fsMimic = requireExtended.mimic('fs', 'querystring');

Whenever a file requires fs module now will receive querystring module.

Mimics can be used custom files too.

const requireExtended = require('require-extended')();
const fsMimic = requireExtended.mimic('fs', './custom-fs.js');

Now the files require fs module will receieve your custom module.

Mimic paths can be used with root path resolver.

const requireExtended = require('require-extended')();
const fsMimic = requireExtended.mimic('fs', '~/custom-fs.js');

Now the files require fs module will receieve your custom module on your project root.

Mimic matchers can be string, regex or a function that returns boolean

const requireExtended = require('require-extended')();
const fsMimic = requireExtended.mimic((path) => {
    return path === 'fs'
}, '~/custom-fs.js');

Mimics can be chained

const requireExtended = require('require-extended')();
const fsMimic = requireExtended.mimic('fs', 'querystring');
const querystringMimic = requireExtended.mimic('querystring', 'http');
const http = require('fs');

Whenever a file require('fs') they will receive http module. Because fs -> querystring -> http.

You can remove mimics by using .restore().

fsMimic .restore();

Testing with require-extended

You can .binding your third-party libraries to make them return your instances.

Sinon

const requireExtended = require('require-extended')();
const sinon = require('sinon');
const fsBinding = requireExtended.binding('registerLibrary', sinon.spy());

You can return custom objects for testing. So you can easily assure that your important require lines are not removed by a coworker.