Here’s a quick way to get code reloading working with Node TypeScript projects. If you’re writing something for the frontend, its pretty easy to get this with webpack, but for the backend you might not want to dip into the webpack world. Instead, we can use typescript’s native watching functionality, plus pm2.

  • Create a pm2 configuration file. I named mine something like devreload.config.json.
{
  "apps": [
    {
      "name": "app",
      "script": "dist/app.js",
      "watch": [ "dist" ]
    },
    {
      "name": "tsc",
      "script": "node_modules/typescript/lib/tsc.js",
      "args": "-w",
      "watch": false
    }
  ]
}

Tweak the script and watch parameters depending on where your compiled typescript code is ending up.

  • Add the following scripts to your package.json.
"scripts": {
  "watch": "pm2 start devreload.config.json",
  "watchLogs": "pm2 logs",
  "watchStop": "pm2 delete devreload.config.json"
}

Done!