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

runnable

v3.0.0

Published

Export a runnable method

Downloads

1,192

Readme

Export a runnable function

NPM Version NPM Downloads Build Status js-happiness-style

Wraps a function so that it is called when run directly but returned when required in. This is especially useful for micro services and and SOA architectures, but can be used for many other things.

Usage

$ npm install runnable

Basic usage:

// index.js

var runnable = require('runnable');

module.exports = runnable(function (opts) {
	console.log(opts.foo);
}, [{
	foo: 'bar'
}], module); // bar
// bin/foo.js

var foo = require('../index.js');

foo({
	foo: 'other bar'
}); // other bar

Usage with multiple runnable instances in one process is a little bit different due to the behavior of node's module.parent implementation which sets the parent to the first module to import the file, as opposed to the actual parent which required it in this time. So to work in this kind of an enviornment you need to pass a final parameter to the runnable call.

var runnable = require('runnable');

module.exports = runnable(function foo () {
	console.log('foo');
}, module);
var runnable = require('runnable');

module.exports = runnable(function bar () {
	console.log('bar');
}, module);
var foo = require('../foo.js');
var bar = require('../bar.js');

foo(); // foo
bar(); // bar

Usage with Browserify

If you are using this module with browserify, it will not work by default. Browserify does not implement module.parent or require.main. Luckily Browserify allows you to provide your own require implementation via the prelude option. This module comes with an implementation which implements the required fields. Hopefully this will get added so Browserify once I make the case. In the mean time you can do this:

var b = browserify('index.js', {
	prelude: require('runnable/browserify-prelude')
}).bundle();

API

runnable(fnc <Function>[,defaults <Array>[, module <Object>]]);
  • fnc: The runnable function
  • defaults: Defaults that will be passed to the function when called directly
  • module: The module the function is declared in, literally module from the commonjs file

Usage in micro-services/SOA

This is an example of how we use this pattern to give production configuration to apps in our SOA setup. In development we just run node index.js, and in prod we run ./bin/server which loads production configuration and registers with our service discovery.

// index.js

var app = require('express')();
var runnable = require('runnable');

module.exports = runnable(function (opts) {
	var server = app.listen(opts.port, function () {
		console.log('Listening on ' + server.addresS().port);
	});
}, [{
	port: 4000
}], module);
// bin/server

var app = require('../');
app({
	port: null // run on a ephemeral port in production
});