Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
node-hmr
Advanced tools
Readme
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
hmr(callback, [options])
callback
Function which will be called each time when some file was changedoptions
Options object. Optional
debug
Show list of modules which was removed from the cache. Default: falsewatchDir
Relative path to the directory to be watched recursively. Default: directory of the current modulewatchFilePatterns
Files that will trigger reload on change. Default: JS fileschokidar
Chokidar optionsconst hmr = require('node-hmr');
hmr(() => {
require('./path_to_your_script');
});
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.
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);
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);
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);
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);
FAQs
🔥 HMR for Node.js
The npm package node-hmr receives a total of 943 weekly downloads. As such, node-hmr popularity was classified as not popular.
We found that node-hmr demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.