命令tsc -w -p server
观看server
目录,将TypeScript编译到dist/server
文件夹中(dist/server/app.js
是主要的Node脚本)。
命令nodemon -w dist/server dist/server/app.js
监视dist/server
文件夹,并在发生更改时重新加载dist/server/app.js
。
问题:如果我同时运行两个命令,tsc
会花费一些时间,但是如果nodemon
不存在,dist/server/app.js
就会启动得太早然而。
concurrently \"tsc -w -p server\" \"nodemon -w dist/server dist/server/app.js\"
另一方面,如果我依次运行命令,则会丢失nodemon
输出(即服务器输出),因为tsc
将监视更改并“窃取”控制台输出:
tsc -w -p server\ && nodemon -w dist/server dist/server/app.js
我已经用nodemon和npm-run-all(一种流行的替代方法)测试了这两种策略。
相关问题(可接受的答案不能解决问题):
答案 0 :(得分:0)
为nodemon
添加1000ms的延迟为我解决了该问题。
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 run code