Nodemon is only for Nodejs. To use hot reload in Nestjs do the following:
STEP1: First install the required packages:
npm i --save-dev webpack-node-externals run-script-webpack-plugin webpack
STEP2: Create a webpack-hmr.config.js file in the root directory of your application
add code:
const nodeExternals = require('webpack-node-externals');
const { RunScriptWebpackPlugin } = require('run-script-webpack-plugin');
module.exports = function (options, webpack) {
return {
...options,
entry: ['webpack/hot/poll?100', options.entry],
externals: [
nodeExternals({
allowlist: ['webpack/hot/poll?100'],
}),
],
plugins: [
...options.plugins,
new webpack.HotModuleReplacementPlugin(),
new webpack.WatchIgnorePlugin({
paths: [/\.js$/, /\.d\.ts$/],
}),
new RunScriptWebpackPlugin({ name: options.output.filename, autoRestart: false }),
],
};
};
STEP3: To enable HMR, open the application entry file (main.ts) and add the following webpack-related instructions:
declare const module: any;
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
if (module.hot) {
module.hot.accept();
module.hot.dispose(() => app.close());
}
}
bootstrap();
STEP4: To simplify the execution process, add a script to your package.json file.
"start:dev": "nest build --webpack --webpackPath webpack-hmr.config.js --watch"
STEP5: Now simply open your command line and run the following command:
npm run start:dev
STEP6: Final Ctrl+S to Hotload
