改一个小工具时,经常是保存、回到终端、再跑一遍。Node 18+ 自带 --watch,简单脚本可以先不用额外装 nodemon。
环境:Node.js 18+。建个文件 watch-demo.mjs:
const now = new Date().toISOString()
console.log('script started at', now)
console.log('edit this file and save, node will restart it')
跑起来:
node --watch watch-demo.mjs
然后随便改一下 watch-demo.mjs 再保存,终端里会重新执行。
这个更适合本地开发时反复试脚本、看输出变化。真到复杂项目,还是项目自己的 dev server 或专门工具更稳一点。