无法让 TypeScript 监视我的项目并让 nodemon 重新加载它 [英] Unable to get TypeScript watching my project and nodemon reloading it
问题描述
Command tsc -w -p server
观察 server
目录编译 TypeScript 到 dist/server
文件夹(dist/server/app.js
是主要的 Node 脚本).
Command tsc -w -p server
watch the server
directory compile TypeScript into dist/server
folder (dist/server/app.js
is the main Node script).
命令 nodemon -w dist/server dist/server/app.js
监视 dist/server
文件夹并重新加载 dist/server/app.js
代码>当事情发生变化时.
Command nodemon -w dist/server dist/server/app.js
watches dist/server
folder and reloads dist/server/app.js
when something changes.
问题:如果我同时运行两个命令,tsc
将需要一些时间,但 nodemon
启动太快,当 dist/server/app.js
还不存在.
The problem: if I run both commands in parallel, tsc
will take some times but nodemon
starts too soon, when dist/server/app.js
doesn't exist yet.
concurrently \"tsc -w -p server\" \"nodemon -w dist/server dist/server/app.js\"
另一方面,如果我按顺序运行 命令,我会丢失 nodemon
输出(即服务器输出),因为 tsc
会注意变化并窃取"控制台输出:
On the other hand, if I run the commands sequentially I lost nodemon
output (that is, the server output) because tsc
will watch for changes and "steal" the console ouput:
tsc -w -p server\ && nodemon -w dist/server dist/server/app.js
我已经使用 nodemon 和 npm-run-all,一种流行的替代方法.
I've tested both these strategies with nodemon and npm-run-all, a popular alternative.
相关问题(接受的答案并不能解决问题):
Related questions (accepted answer doesn't solve the problem):
推荐答案
向 nodemon
添加 1000 毫秒的延迟为我解决了这个问题.
Adding a delay of 1000ms to nodemon
fixed the issue for me.
https://github.com/remy/nodemon#delaying-restarting
nodemon.json
{
"watch": ["build"],
"ext": "js",
"exec": "npm start",
"delay": 1000
}
package.json
{
"name": "demo",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node build/index.js",
"ts": "tsc -w",
"nodemon": "nodemon",
"code": "concurrently -n ts,nodemon npm:ts npm:nodemon"
},
"devDependencies": {
"concurrently": "^4.1.0",
"nodemon": "^1.18.9",
"typescript": "^3.2.2"
}
}
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"target": "es2017",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "build",
"baseUrl": ".",
"paths": {
"*": [
"node_modules/*",
"src/types/*"
]
}
},
"include": [
"src/**/*"
]
}
npm 运行代码
这篇关于无法让 TypeScript 监视我的项目并让 nodemon 重新加载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!