Nest.js框架热重载不起作用

Jos*_*lez 5 node.js webpack-dev-server nestjs

我已按照文档中的步骤操作:

\n\n

https://docs.nestjs.com/techniques/hot-reload

\n\n

我正在运行此命令:npm run webpack但它关闭了,它返回了提示,并且不会继续监视文件:

\n\n
    gabriel@roraima-tv:/var/www/studying/tera-ping-pong$ npm run webpack\n\n    > tera-ping-pong@0.0.0 webpack /var/www/studying/tera-ping-pong\n    > webpack --config webpack.config.js\n\n\n    webpack is watching the files\xe2\x80\xa6\n\n    Hash: 6e13d56ba7d77331e5c2\n    Version: webpack 4.23.1\n    Time: 3014ms\n    Built at: 11/01/2018 1:39:11 PM\n                       Asset       Size  Chunks             Chunk         Names\n    dist/app.controller.d.ts  177 bytes          [emitted]  \n        dist/app.module.d.ts   35 bytes          [emitted]  \n       dist/app.service.d.ts   56 bytes          [emitted]  \n              dist/main.d.ts   11 bytes          [emitted]  \n          dist/main.hmr.d.ts   11 bytes          [emitted]  \n                   server.js     39 KiB    main  [emitted]  main\n    Entrypoint main = server.js\n    [0] multi webpack/hot/poll?1000 ./src/main.hmr.ts 40 bytes {main}         [built]\n    [./node_modules/webpack/hot/log-apply-result.js]         (webpack)/hot/log-apply-result.js 1.27 KiB {main} [built]\n    [./node_modules/webpack/hot/log.js] (webpack)/hot/log.js 1.11 KiB         {main} [built]\n    [./node_modules/webpack/hot/poll.js?1000] (webpack)/hot/poll.js?        1000 1.15 KiB {main} [built]\n    [./src/app.controller.ts] 1.44 KiB {main} [built]\n    [./src/app.module.ts] 1.03 KiB {main} [built]\n    [./src/app.service.ts] 883 bytes {main} [built]\n    [./src/main.hmr.ts] 1.07 KiB {main} [built]\n    [@nestjs/common] external "@nestjs/common" 42 bytes {main} [built]\n    [@nestjs/core] external "@nestjs/core" 42 bytes {main} [built]\n    gabriel@roraima-tv:/var/www/studying/tera-ping-pong$ \n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,每当我添加 *.ts 文件更改时,它们在服务器重新启动之前不会重新加载。

\n

Vin*_*nci 17

你可以在 CLI 中使用这个命令,它是默认的:

npm run start:dev
Run Code Online (Sandbox Code Playgroud)


Ach*_*aul 12

您可以在监视模式下运行 Nest

nest start --watch
Run Code Online (Sandbox Code Playgroud)

  • 如果我错了,请纠正我,但是,虽然它的工作原理与热重载类似,但我相信这个命令行只是重新启动整个 NestJS 应用程序,它的工作方式与热重载不同,它只重新加载您更改的特定文件。它完成了工作,坦率地说,这对我来说已经足够了,但它并不完全是一个热重载功能。这就是为什么我相信这个答案没有被选为正确答案。 (2认同)

小智 4

首先安装所需的包:

npm i --save-dev webpack-node-externals start-server-webpack-plugin
Run Code Online (Sandbox Code Playgroud)

安装完成后,在应用程序的根目录中创建一个 webpack-hmr.config.js 文件。

 const webpack = require('webpack');
const nodeExternals = require('webpack-node-externals');
const StartServerPlugin = require('start-server-webpack-plugin');

module.exports = function(options) {
  return {
    ...options,
    entry: ['webpack/hot/poll?100', options.entry],
    watch: true,
    externals: [
      nodeExternals({
        allowlist: ['webpack/hot/poll?100'],
      }),
    ],
    plugins: [
      ...options.plugins,
      new webpack.HotModuleReplacementPlugin(),
      new webpack.WatchIgnorePlugin([/\.js$/, /\.d\.ts$/]),
      new StartServerPlugin({ name: options.output.filename }),
    ],
  };
};
Run Code Online (Sandbox Code Playgroud)

要启用HMR,请打开应用程序入口文件(main.ts)并添加以下webpack相关指令:

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();
Run Code Online (Sandbox Code Playgroud)