Socket
Socket
Sign inDemoInstall

node-hot-loader

Package Overview
Dependencies
Maintainers
1
Versions
93
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-hot-loader

Hot module replacement for Node.js applications


Version published
Weekly downloads
552
decreased by-36.41%
Maintainers
1
Weekly downloads
 
Created
Source

Node Hot Loader npm package

Node Hot Loader is a small tool written on ES2015+ for Hot Module Replacement support for Node.js application development with webpack.

Its inspired by kotatsu and webpack-dev-middleware.

Typical use cases for Node Hot Loader are hot-reloaded express application with APIs and frontend serving, i.e. React.

Node Hot Loader support webpack config files with ES2015+ (through babel). For using ES2015+ in webpack configuration you must provide .babelrc configuration file in project root directory.

Installation

npm install --save-dev node-hot-loader

Usage

Usage: node-hot {options}

Options:
  -c, --config       Webpack config file. If not set then search webpack.config.js in root directory.

Example

node-hot --config webpack.config.server.babel.js

You can use all configurations for webpack compile which webpack supports.

The minimum required configuration:

{
  // It's required!
  // Also if you use multiconfigurations node-hot choose configuration with target 'node'.
  target: 'node',
  
  // For now you must provide entry with 'server' name which will be the main entry point of node application.
  // It will be fixed in the feature.
  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(),
  ],
}

Express example

import app from './app'; // configuring express app, i.e. 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);
    });

Known limitations

In your webpack config you must provide main entry with 'server' name. It will be fixed in the feature.

License

MIT (https://opensource.org/licenses/mit-license.php)

Keywords

FAQs

Package last updated on 17 May 2017

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc