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

with-autoreloading

v0.0.0

Published

Require a module in the REPL that attempts to reload itself when it is changed

Downloads

5

Readme

With Autoreloading

Require a magically self-reloading module. Useful if you want to try out exploratory changes in the REPL to code already in a file on disk.

// test.js
module.exports = function() {
  return 'old';
};

// REPL
> var test = require('with-autoreloading')('./test');
> test();
'old'

// test.js, edited and saved
module.exports = function() {
  return 'new';
}

// same REPL
> test();
'new'
So how far did you go with this
// numbers.js
module.exports = {
  numbers: [ 1, 2, 3 ],
  multiply: function() {
    return this.numbers.map(function(num) {
      return num * 2;
    })
  }
}

// REPL
> var numbers = require('with-autoreloading')('./numbers')
> numbers.numbers
[ 1, 2, 3 ]
> numbers.multiply()
[ 2, 4, 6 ]

// numbers.js, edited & saved
module.exports = {
  numbers: [ 4, 5 ],
  multiply: function() {
    return this.numbers.map(function(num) {
      return num * 3;
    })
  }
}

// same REPL
> numbers.numbers
[ 4, 5 ]
> numbers.multiply()
[ 9, 15 ]
OK cool. Tell me more.
// car.js
function Car() {};

Car.prototype.wheels = 4;

Car.prototype.replaceTires = function() {
  if(this.wheels != 4) console.error('that\'s a weird number of wheels...');
  return this.wheels + ' tires replaced!';
};

module.exports = Car;

// REPL
> var Car = require('with-autoreloading')('./car');
> var myCar = new Car();
> myCar.replaceTires();
'4 tires replaced!'

// edited car.js
(snip)
Car.prototype.wheels = 6;
(snip)

// same REPL
> myCar.replaceTires()
that's a weird number of wheels...
'6 tires replaced!'
No friggin way

Yeah seriously.

So how does it do that

A middleman proxy object/function. When you save the file, it clears the require cache for that file, requires it again, and has the proxy call through to the new code.

Could I use this in production?

Please don't. Use like, supervisor or nodemon or something more like that. This is aimed at the REPL, and even there I'm not convinced it's 100% stable/correct (but useful enough despite that).

Tests

npm test