我有一个用TypeScript编写的非常简单的应用程序:
src/index.ts
import * as http from "http";
const server = http.createServer((request, response) =>
{
response.end("Hello, World");
});
server.listen(3000);
然后我的TypeScript配置:
tsconfig.json
{
"compilerOptions": {
// Output options
"target": "es5",
"lib": [
"es2016"
],
"module": "commonjs",
"outDir": "./build",
}
}
我可以使用npx tsc
构建我的代码,然后使用node ./build/index.js
运行它http://localhost:3000在浏览器中,我看到消息"你好,世界"——到目前为止一切都很好
现在使用npx tsc -w
,我可以观察文件是否发生了更改,并在发生这种情况时重新编译它们。这个命令运行"永远"(直到停止(,所以它阻止我在同一个终端中运行node ./output/index.js
。使用多个终端窗口或终端多路复用器,我可以非常轻松地运行node ./output/index.js
,但如果我的代码被重新编译,这个文件就不会重新运行。这意味着,如果我将字符串"Hello,World"更改为"Hello、Steven",在手动停止服务器并重新启动之前,我不会看到更改
有没有一种方法可以监视我的TypeScript文件(如和(运行输出,以便在我的代码更改时停止并重新运行输出?
您可以同时运行tsc
和nodemon
:
npx tsc -w & npx nodemon build
或将nodemon与ts节点一起使用:
npx nodemon -x ts-node -e ts src
# to run even if there are TS errors:
npx nodemon -x 'ts-node --log-error' -e ts src
或者只使用ts-node-dev
:
npx ts-node-dev src