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

node-hmr

v1.4.0

Published

🔥 HMR for Node.js

Downloads

5,967

Readme

node-hmr

Build Status Coverage Status License: MIT

🔥 Hot Module Replacement for Node.js

This module adds hot module replacement support for node.js applications, it intended as an alternative to such tools like nodemon. Reloading modules while an application is running significantly faster than full reloading which in some cases may add additional downtime to a development process.

Inspired by this article https://codeburst.io/dont-use-nodemon-there-are-better-ways-fc016b50b45e

API

hmr(callback, [options])
  • callback Function which will be called each time when some file was changed
  • options Options object. Optional
    • debug Show list of modules which was removed from the cache. Default: false
    • watchDir Relative path to the directory to be watched recursively. Default: directory of the current module
    • watchFilePatterns Files that will trigger reload on change. Default: JS files
    • chokidar Chokidar options

Usage

const hmr = require('node-hmr');

hmr(() => {
  require('./path_to_your_script');
});

How to use it with frameworks

You should split your application into two parts first is server setup and second is application module. Below are examples of how to use it with some popular frameworks.

Typescript + Express.js example

app.ts

import express, { Application, Request, Response, NextFunction, } from 'express';
const app: Application = express();

app.get('/', (req: Request, res: Response, next: NextFunction) => {
  res.send('Express app');
});

export default app;

index.ts

import http from 'http';
import hmr from 'node-hmr';

let app: http.RequestListener;

hmr(async () => {
  console.log('Reloading app...');
  ({ default: app } = await import('./app'));
});

const server = http.createServer((req, res) => app(req, res));

server.listen(3000);

Express.js example

app.js

const express = require('express');
const app = express();

app.get('/', (req, res, next) => {
  res.send('Express');
});

module.exports = app;

bin/www

const http = require('http');
const hmr = require('node-hmr');

let app;

hmr(() => {
  app = require('../app');
}, { watchDir: '../', watchFilePatterns: ['**/*.js'] });

const server = http.createServer((req, res) => app(req, res));

server.listen(3000);

Koa.js with HMR example

app.js

const Koa = require('koa');
const app = new Koa();

app.use(async ctx => {
  ctx.body = 'Koa';
});

module.exports = app;

index.js

const hmr = require('node-hmr');

let callback;
hmr(() => {
  const app = require('./app');
  callback = app.callback();
});

const server = http.createServer((req, res) => callback(req, res));
server.listen(3000);

Limitations

In some cases, HMR may not work correctly with libraries which using some internal caching storage Mongoose is the example of one. If you see this error message Cannot overwrite `User` model once compiled the workaround could be add the following syntax to each model declaration, but in this case changes to the model will not be 'hot reloaded'.

module.exports = mongoose.models.Users || mongoose.model('Users', UsersSchema);

License

MIT