The last few months I have been involved in many Backend projects and it is always interesting to see different approaches that people follow when setting things up. I realised that there are many arguments on the very first step of “how do I run my server”. Every person I meet honestly has different ways of doing that and im like “chillax people… it’s just a command” but you know how sensitive our community is. The one library that has never disappointed me is Nodemon.
Nodemon is great for running projects locally as it allows you to do cool stuff like hot reload and works great with typescript. My favourite configuration goes as follows. Open your package.json
file and add the following line into your scripts.
{
"dev": "nodemon"
}
That way we keep things clean (yes I am talking to you devs that add 20 environment variables in per script) and we have a separate config file that allows us to add whatever we want outside the package.json file. Let’s create a new config file for nodemon.json
and add the following:
{
"watch": ["src/server/*"],
"exec": "node src/server"
}
Yes it is that simple! Make sure that you are watching the right folder so that hot reload will work fine! You specify the path(s) inside watch
which expects an array of paths. That way every time you save something in any of these paths your server will restart and woohoo you saved a couple of seconds of your life 🎉. You can then specify the commands you want to run in exec
. This works great for TypeScript servers as well. Adding "ext": "ts"
will tell the server that it also needs to be watching for TypeScript files.
{
"watch": ["src/server/*"],
"ext": "ts",
"exec": "ts-node --project tsconfig.json src/server"
}
However hot reload can sometimes cause a bit of trouble.