gpt4 book ai didi

nestjs - nest start --watch 更改后不重新加载(nest start --watch 不工作)

转载 作者:行者123 更新时间:2023-12-05 05:46:38 77 4
gpt4 key购买 nike

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

enter image description here

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

enter image description here

我确信这不是我的代码的问题,因为我在我的所有 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

Hot Reload with webpack

关于nestjs - nest start --watch 更改后不重新加载(nest start --watch 不工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71138325/

77 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com