Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
node-hot-loader
Advanced tools
Node Hot Loader is a small tool written on ES2015+ for Hot Module Replacement support for Node.js application development with webpack.
It based on sources of webpack-dev-middleware and webpack/hot/only-dev-server. Under the hood it uses webpack and babel, so you can use all you need configurations in config files for babel and webpack.
The most suitable use case for Node Hot Loader is hot-reloaded express application. Express application can contains API and frontend together, moreover frontend can use own HMR, e.g. React with React Hot Loader. See how to setup React HMR with Express in React Hot Loader docs. Thus, both the frontend and the server will be hot-reloadable.
Node Hot Loader supports webpack config files with ES2015+ (through babel). For using ES2015+ in webpack configuration you must provide .babelrc configuration file in project root directory.
Tested with Node.js v7, but must work on previous versions.
npm install --save-dev node-hot-loader
Usage: node-hot {options}
Options:
-c, --config Webpack config file. If not set then search webpack.config.js in root directory.
node-hot --config webpack.config.server.js
You can use all configurations for webpack compiler which webpack supports.
{
// It's required!
// Also if you use multiconfigurations node-hot choose configuration with target 'node'.
target: 'node',
// node-hot run the last entry which must be the main entry point of node application.
entry: {
server: [
'./server/index',
],
},
plugins: [
// It's required!
// Enable HMR globally.
new webpack.HotModuleReplacementPlugin(),
// It's not necessary.
// Prints more readable module names in the console on HMR updates.
new webpack.NamedModulesPlugin(),
// It's not necessary.
// In order for don't emit files if errors occurred.
new webpack.NoEmitOnErrorsPlugin(),
],
// It may be necessary when your compiled app uses Webpack too, e.g. for frontend serving,
// because Webpack sets __dirname to '/'.
// It may be some issues in your app, so sets __dirname to false can help you.
// See https://github.com/webpack/webpack/issues/1599.
node: {
__dirname: false,
__filename: false,
},
}
import app from './app'; // configuring express app, e.g. routes and logic
import DB from './services/DB'; // DB service
function startServer() {
const httpServer = app.listen(app.get('port'), (error) => {
if (error) {
console.error(error);
} else {
const address = httpServer.address();
console.info(`==> 🌎 Listening on ${address.port}. Open up http://localhost:${address.port}/ in your browser.`);
}
});
// Hot Module Replacement API
if (module.hot) {
let currentApp = app;
module.hot.accept('./app', () => {
httpServer.removeListener('request', currentApp);
import('./app').then(m => {
httpServer.on('request', m.default);
currentApp = m.default;
console.log('Server reloaded!');
})
.catch(console.error);
});
}
}
// After DB initialized start server
DB.connect()
.then(() => {
console.log('Successfully connected to MongoDB. Starting http server...');
startServer();
})
.catch((err) => {
console.error('Error in server start script.', err);
});
FAQs
Hot module replacement for Node.js applications
We found that node-hot-loader demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers 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
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.