Hot Reload

The highest impact on your application’s bootstrapping process is TypeScript compilation. Fortunately, with webpack HMR (Hot-Module Replacement), we don’t need to recompile the entire project each time a change occurs. This significantly decreases the amount of time necessary to instantiate your application, and makes iterative development a lot easier.

warning Warning Note that webpack won’t automatically copy your assets (e.g. graphql files) to the dist folder. Similarly, webpack is not compatible with glob static paths (e.g., the entities property in TypeOrmModule).

With CLI

If you are using the Nest CLI, the configuration process is pretty straightforward. The CLI wraps webpack, which allows use of the HotModuleReplacementPlugin.

Installation

First install the required packages:

  1. $ npm i --save-dev webpack-node-externals run-script-webpack-plugin webpack

info Hint If you use Yarn Berry (not classic Yarn), install the webpack-pnp-externals package instead of the webpack-node-externals.

Configuration

Once the installation is complete, create a webpack-hmr.config.js file in the root directory of your application.

  1. const nodeExternals = require('webpack-node-externals');
  2. const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
  3. module.exports = function (options, webpack) {
  4. return {
  5. ...options,
  6. entry: ['webpack/hot/poll?100', options.entry],
  7. externals: [
  8. nodeExternals({
  9. allowlist: ['webpack/hot/poll?100'],
  10. }),
  11. ],
  12. plugins: [
  13. ...options.plugins,
  14. new webpack.HotModuleReplacementPlugin(),
  15. new webpack.WatchIgnorePlugin({
  16. paths: [/\.js$/, /\.d\.ts$/],
  17. }),
  18. new RunScriptWebpackPlugin({ name: options.output.filename }),
  19. ],
  20. };
  21. };

info Hint With Yarn Berry (not classic Yarn), instead of using the nodeExternals in the externals configuration property, use the WebpackPnpExternals from webpack-pnp-externals package: WebpackPnpExternals({{ '{' }} exclude: ['webpack/hot/poll?100'] {{ '}' }}).

This function takes the original object containing the default webpack configuration as a first argument, and the reference to the underlying webpack package used by the Nest CLI as the second one. Also, it returns a modified webpack configuration with the HotModuleReplacementPlugin, WatchIgnorePlugin, and RunScriptWebpackPlugin plugins.

Hot-Module Replacement

To enable HMR, open the application entry file (main.ts) and add the following webpack-related instructions:

  1. declare const module: any;
  2. async function bootstrap() {
  3. const app = await NestFactory.create(AppModule);
  4. await app.listen(3000);
  5. if (module.hot) {
  6. module.hot.accept();
  7. module.hot.dispose(() => app.close());
  8. }
  9. }
  10. bootstrap();

To simplify the execution process, add a script to your package.json file.

  1. "start:dev": "nest build --webpack --webpackPath webpack-hmr.config.js --watch"

Now simply open your command line and run the following command:

  1. $ npm run start:dev

Without CLI

If you are not using the Nest CLI, the configuration will be slightly more complex (will require more manual steps).

Installation

First install the required packages:

  1. $ npm i --save-dev webpack webpack-cli webpack-node-externals ts-loader run-script-webpack-plugin

info Hint If you use Yarn Berry (not classic Yarn), install the webpack-pnp-externals package instead of the webpack-node-externals.

Configuration

Once the installation is complete, create a webpack.config.js file in the root directory of your application.

  1. const webpack = require('webpack');
  2. const path = require('path');
  3. const nodeExternals = require('webpack-node-externals');
  4. const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
  5. module.exports = {
  6. entry: ['webpack/hot/poll?100', './src/main.ts'],
  7. target: 'node',
  8. externals: [
  9. nodeExternals({
  10. allowlist: ['webpack/hot/poll?100'],
  11. }),
  12. ],
  13. module: {
  14. rules: [
  15. {
  16. test: /.tsx?$/,
  17. use: 'ts-loader',
  18. exclude: /node_modules/,
  19. },
  20. ],
  21. },
  22. mode: 'development',
  23. resolve: {
  24. extensions: ['.tsx', '.ts', '.js'],
  25. },
  26. plugins: [
  27. new webpack.HotModuleReplacementPlugin(),
  28. new RunScriptWebpackPlugin({ name: 'server.js' }),
  29. ],
  30. output: {
  31. path: path.join(__dirname, 'dist'),
  32. filename: 'server.js',
  33. },
  34. };

info Hint With Yarn Berry (not classic Yarn), instead of using the nodeExternals in the externals configuration property, use the WebpackPnpExternals from webpack-pnp-externals package: WebpackPnpExternals({{ '{' }} exclude: ['webpack/hot/poll?100'] {{ '}' }}).

This configuration tells webpack a few essential things about your application: location of the entry file, which directory should be used to hold compiled files, and what kind of loader we want to use to compile source files. Generally, you should be able to use this file as-is, even if you don’t fully understand all of the options.

Hot-Module Replacement

To enable HMR, open the application entry file (main.ts) and add the following webpack-related instructions:

  1. declare const module: any;
  2. async function bootstrap() {
  3. const app = await NestFactory.create(AppModule);
  4. await app.listen(3000);
  5. if (module.hot) {
  6. module.hot.accept();
  7. module.hot.dispose(() => app.close());
  8. }
  9. }
  10. bootstrap();

To simplify the execution process, add a script to your package.json file.

  1. "start:dev": "webpack --config webpack.config.js --watch"

Now simply open your command line and run the following command:

  1. $ npm run start:dev

Example

A working example is available here.

TypeORM

If you’re using @nestjs/typeorm, you’ll need to add keepConnectionAlive: true to your TypeORM configuration.