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

fequire

v0.5.0

Published

require() replacement, with possible custom context, and possible async

Downloads

4

Readme

Fequire

A require() replacement that doesn't cache modules and is pretty nifty for doing all kinds of manipulations. Fequire stands for "fake require". Yeah.

Fequire loads the module specified by the path you set, and runs it in a virtual machine (using the native vm node module), with a context you provide. The default context mimicks the node environment by providing require(), module, and console, and anything you pass will be mixed in.

You can also pass in your custom context your own require or console to override default functionality.

Lastly, fequire can work async, which can be useful if you're loading modules dynamically.

Usage

npm install -s fequire
//module.js
var fs = require('fs');
var file = fs.readFileSync('./some.file')
module.exports = add(1,2);
//index.js
var fequire = require('fequire');
var module = fequire('./module.js',{
    add:function(a,b){return a+b;}
});

The add function has been passed as a context, so it is available in the global scope of module.js. module.js can also use the fs module normally, because require is part of the default context. But you could override it:

//index.js
var fequire = require('fequire');
var module = fequire('./module.js',{
    add:function(a,b){return a+b;}
,   require:function(name){
        if(name.charAt(0) == '.'){
            return require(fequire.resolve('./',name));
        }
        if(name=='fs'){return require('fake-fs');}
        return require('fs');
    }
});

In this instance, if the module asked for fs, it would get fake-fs and would be none the wiser. As an aside, fequire.resolve is a simple function to get a path relative to the module location.


What if you wanted to manipulate the text of the module before evaluating it? For example, to use your own macros? In this case, use fequire.run:

//module.js
module.exports = function(){
    args = ##arguments##;
    console.log(args);
}
//index.js
var fequire = require('fequire');
var fs = require('fs');
var moduleText = fs.readFileSync('./module.js',{encoding:'utf8'});
moduleText = moduleText.replace('##arguments##','Array.prototype.slice.call(arguments);');
var module = fequire.run('./module.js',moduleText);

API

###FEQUIRE() Loads modules

###fequire(path) Equivalent to require(), but without caching.

###fequire(path,context) Equivalent to require(), but with a custom 'context' object you provide.

###fequire(path,callback) Async require(). the callback argument is a function with the following signature:

function callback(err,returnedValue){}

Where err is any error encountered, and returnedValue is what the module returned, either by using exports= or module.exports =.

###fequire(path,context,callback) Async require() with a custom 'context' object you provide.


###FEQUIRE.RUN Creates modules out of text you provide.

###fequire.run(path,text) Equivalent to require(), but you are tasked to load the module text (or generate it) and pass it to the function as the text argument. The path argument is only used for error reporting.

###fequire.run(path,text,context) Equivalent to fequire(path,context): provides a custom context to the module you provide.

###fequire.run(path,text,callback) It doesn't make much sense (after all the module is already loaded, since you pass the text yourself), but for the sake of consistency, this is the equivalent to fequire(path,callback): async module loading.

###fequire.run(path,text,context,callback) Same as above, but with a custom context.


TODO

tests!


License

MIT