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

postrequire

v2.0.0

Published

(Re)runs initialization code contained in a Node.js module.

Downloads

158

Readme

postrequire · npm version

Overview

postrequire is a Node.js utility that can load a module multiple times, rerunning any initialization code contained in that module. The newly created module is discarded immediately, without being stored in the module cache or added to the children list of the parent module. If the new module calls require to require other modules, those modules will be loaded and cached normally, and any initialization code they contain will only run when the modules are required for the first time.

var postrequire = require("postrequire");
// Like `require("./my-module")`, except new module is not cached.
var obj1 = postrequire("./my-module");
// ...
var obj2 = postrequire("./my-module"); // Rerunning initialization code.

Stubbing CommonJS variables

It is also possible to modify the values of the CommonJS global‐like variables exports, require, module, __filename and __dirname inside the module being loaded with custom values. The value of this at module level can be modified, too.

In order to modify the value of one or more of the identifiers inside the new modules, a second parameter must be passed to postrequire.

If the second parameter is a regular object, the values of any defined properties having the name of a CommonJS variable or this will be used to replace the values of their respective identifiers inside the module.

Another option is passing a callback function as a second parameter. The callback is called with an object as a single argument before the module is loaded. The object passed to the callback contains name‐value property mappings for all CommonJS variables and this as they would regularly occur inside the new module. If the property values are modified inside the callback, the changes are reflected when the module is loaded.

Example 1: Faking __filename and __dirname

// real-module.js
console.log("The pathname of this module is " + __filename);
console.log("This module is located inside " + __dirname);
// main.js
var postrequire = require("postrequire");
postrequire("./real-module", { __filename: "/tmp/fake-module.js", __dirname: "/tmp" });

node main.js outputs

The pathname of this module is /tmp/fake-module.js
This module is located inside /tmp

Example 2: Transitive postrequire

function withTransitivePostrequire(stubs)
{
    var require = stubs.require;
    var postrequire = require("postrequire");
    function transitivePostrequire(id)
    {
        return postrequire(id, withTransitivePostrequire);
    }
    for (var key in require)
        transitivePostrequire[key] = require[key];
    stubs.require = transitivePostrequire;
}

var postrequire = require("postrequire");
// Load a module and all its descendant modules without using the cache.
// NOTE: This will result in a stack overflow error if any circular dependencies are encountered.
var imports = postrequire("semver", withTransitivePostrequire);

Example 3: Pretending to be a browser with JSDOM

// browser.js
if (typeof module !== "undefined")
    throw Error("This script can only run in a browser. Node.js/CommonJS found!");
if (typeof window !== "undefined")
{
    console.log("Browser window found!");
    // ...
}
// main.js
const jsdom = require("jsdom");
const postrequire = require("postrequire");
const window = new jsdom.JSDOM("");
Object.setPrototypeOf(global, window); // Make properties of the window object available globally.
postrequire
(
    "./browser.js",
    {
        this: window,
        exports: undefined,
        require: undefined,
        module: undefined,
        __filename: undefined,
        __dirname: undefined,
    }
);

node main.js outputs

Browser window found!

Other similar software

import-fresh

  • Does not run in older versions of Node.js.

proxyquire

  • Adds new modules to the children list of the parent module.
  • Creates new exports when required multiple times in the same module, i.e. require("proxyquire") !== require("proxyquire") always.
  • Always needs second parameter stubs.

rewire

  • Adds new modules to the children list of the parent module.
  • Creates new exports when required multiple times in the same module, i.e. require("rewire") !== require("rewire") always.
  • Does not run in older versions of Node.js.

Stealthy-Require

  • Transitively reloads all modules required by another module at load time.
  • Adds new modules to the children list of the parent module.
  • Complicated usage.