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

uncache

v0.1.0

Published

non-caching require() for live coding

Downloads

66

Readme

uncache

Like require(), but do not put the required package into the cache to facilitate live coding (actually, it removes the required module from the cache!).

What Can You Do?

  • Create a web server that you don't have to restart when you change the code.

Usage

var live = require('uncache')(require)

Now, use live('module') instead of require('module'). It will load the file each time.

When the module exports a function

var func = live.function('./module')

When func() is called, it will reload the module before calling it. Of course, this assumes that the module exports a function.

Use Cases

Restartless Express Application

One annoying thing when creating web applications with Node.js is that you have to restart the server when you change the code.

Even with nodemon that restarts the server automatically, it's still annoying.

Since the app returned by express() is a function that can be passed to http.createServer, you can create a web server like this:

var live = require('uncache')(require)
var http = require('http')

http.createServer(live.function('./app'))
    .listen(3002, '127.0.0.1')

And app.js like this:

var express = require('express')
var app = module.exports = express()

app.get('/', function(req, res, next) {
  res.send('hello')
})

Each time you send a request to this server, a new application is created.

That means when you change the code, for example, from 'hello' to 'world', and then refresh the page.

Going live: when you want to go production, just change live.function to require, and it's done!

What It Does Not Do

This module is only concerned about making require() clear the cache.

It does not have these features:

Switching between live and require.

Create your own abstraction, such as:

function load(module) {
  return (process.env.NODE_ENV == 'production') ? require(module) : live(module)
}

Do some cleanup when a module is reloaded.

Maybe you can use a global variable for this. Here is a cleanup function:

function cleanup(module, fn) {

  var key = 'cleanup:' + module.id

  // If there is a cleanup function for this module,
  // this means the module is loaded, so, call it:
  if (global[key]) {
    global[key]()
  }

  // Register the cleanup function.
  global[key] = fn

}

When cleanup is called the second time (when the module is reloaded), it will detect the cleanup function from the first load and run it.

console.log('Start up!')

cleanup(module, function() {
  console.log('Shut down!')
})