Have you ever been struggling to reproduce a weird bug happening only on production?
Some offset between what you have in local (that obviously works perfectly) and what your clients see on this evil, inaccessible server?
If you're using anything other than Windows Server, there are chances your server is using the UTC timezone, which is not - unless you live in England - the same timezone than your computer.
The simplest solution to reproduce what's happening on your server is to set your timezone to UTC. And there is a way to run node in UTC without changing your computer timezone!
To use UTC as your node timezone, you can simply set it in the env before running node, like so :
TZ=utc node index.js
It also works with other timezones, for instance this would set the timezone to Amsterdam's one:
TZ='Europe/Amsterdam' node server/index.js
I recommand setting it in your package.json to always develop to stay as close as possible to production's conditions. In my package.json it looks like this :
{
"scripts": {
"dev": "TZ=utc nodemon --max_old_space_size=8192 server/index.js",
}
}
And if you're not using UTC on production, you should definitely read The worst Server Setup Mistake You Can Make.
Good luck debugging your timezone issues !
Top comments (3)
Thank you. TypeORM was returning 3 hours ahed from timestamp column fields. This env fixed the problem.
Thank you so much !
You have just rescued my Sunday afternoon !
Hi Raphaël,
I want the timezone to be based on location to account for timezone shift, how can i do that?