You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将Node.js的tsc构建监听与nodemon合并为单脚本?

合并TypeScript监听与自动运行的npm脚本方案

方案1:用concurrently并行执行两个监听命令

这是最直观的解决方式,能让TypeScript编译监听和产物运行监听在同一个终端里同时工作:

  • 先安装依赖:
npm install concurrently --save-dev
  • 修改package.json里的scripts:
"scripts": {
  "build:watch": "tsc -p . -w",
  "start:watch": "nodemon dist/index.js",
  "start": "concurrently \"npm run build:watch\" \"npm run start:watch\""
}

执行npm start就能同时启动两个监听任务,代码变更时会自动编译并重启运行。

方案2:用ts-node-dev一步搞定(更简洁)

如果不想分开管理编译和运行的监听,ts-node-dev可以直接监听TypeScript源文件,自动完成编译+重启进程的工作:

  • 安装依赖:
npm install ts-node-dev --save-dev
  • 修改scripts:
"scripts": {
  "start": "ts-node-dev --respawn src/index.ts"
}

它会自动处理编译逻辑,不需要手动维护dist目录的监听,适合快速开发场景。

方案3:用npm-run-all作为并行工具替代

和concurrently功能类似,用npm-run-all也能实现并行运行脚本:

  • 安装依赖:
npm install npm-run-all --save-dev
  • 修改scripts:
"scripts": {
  "build:watch": "tsc -p . -w",
  "start:watch": "nodemon dist/index.js",
  "start": "run-p build:watch start:watch"
}

run-p是npm-run-all的并行命令缩写,效果和concurrently一致。

内容的提问来源于stack exchange,提问作者drin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 14:14:57