嵌套启动 --watch 更改后不重新加载(嵌套启动 --watch 不工作)

2024-04-16

我安装了 Nest.js。当我运行 npm run start:dev (运行 start --watch)时,一切正常并且出现绿色日志。

问题是,当我更新代码中的某些内容时,nest 不再更新,并且卡在下图中:

我确信这不是我的代码的问题,因为我在所有的 Nest.js 存储库中都遇到了同样的问题。我还删除了node_modules并重新安装它们,但它不起作用。

我还尝试在全局范围内重新安装 Nest CLI。

我的节点版本是16.5.0和npm 8.5.0

这是我的 package.json:

{
  "name": "unigow-backend",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "env-cmd -f .env.production rimraf dist",
    "build": "env-cmd -f .env.production nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "env-cmd -f .env.development nest start --watch",
    "start:debug": "env-cmd -f .env.development nest start --debug --watch",
    "start:prod": "env-cmd -f .env.production node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@nestjs/common": "^8.3.0",
    "@nestjs/core": "^8.0.0",
    "@nestjs/mapped-types": "^1.0.1",
    "@nestjs/mongoose": "^8.0.1",
    "@nestjs/platform-express": "^8.0.0",
    "@types/dotenv": "^8.2.0",
    "@types/luxon": "^2.0.9",
    "@types/mongoose": "^5.11.97",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.13.2",
    "dateformat": "^5.0.1",
    "dotenv": "^10.0.0",
    "env-cmd": "^10.1.0",
    "luxon": "^1.28.0",
    "moment": "^2.29.1",
    "moment-range": "^4.0.2",
    "mongoose": "^5.13.9",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rrule": "^2.6.8",
    "rxjs": "^7.2.0",
    "sib-api-v3-sdk": "^8.2.1",
    "stripe": "^8.183.0",
    "twilio": "^3.69.0",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "@nestjs/cli": "^8.0.0",
    "@nestjs/schematics": "^8.0.0",
    "@nestjs/testing": "^8.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "^26.0.24",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^4.28.2",
    "@typescript-eslint/parser": "^4.28.2",
    "eslint": "^7.30.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^3.4.0",
    "jest": "27.0.6",
    "prettier": "^2.3.2",
    "supertest": "^6.1.3",
    "ts-jest": "^27.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "^3.10.1",
    "typescript": "^4.3.5"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

这是我的 ts 配置:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true,
    "skipLibCheck": true
  }
}

这是我的 tsconfig.build:

{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

我对nestjs也有同样的问题。

我不知道到底是什么问题。

但这就是我解决它的方法。

首先,安装所需的软件包:

npm i --save-dev webpack-node-externals run-script-webpack-plugin webpack

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

/* webpack-hmr.config.js file */
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 }),
    ],
  };
};

打开应用程序入口文件(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();

为了简化执行过程,请将脚本添加到 package.json 文件中。

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

现在打开命令行并运行以下命令:

npm run dev

使用 webpack 热重载 https://docs.nestjs.com/recipes/hot-reload

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

嵌套启动 --watch 更改后不重新加载(嵌套启动 --watch 不工作) 的相关文章

随机推荐