Scientists appear to have traced the irresistible pull of the refrigerator at 2am to the "night munchies" gene.
When the PER1 gene is faulty, the natural mechanism that synchronises sleeping and eating goes awry, they believe.
This can lead to "night eating syndrome", the inability to avoid feeling hungry at night which in some people can disrupt sleep and lead to over-eating and weight gain.
The discovery was made by conducting tests on mice with two human genes, PER1 and its partner PER2, which has previously been linked to sleep disturbances.
When PER2 was defective in the mice, as expected they dozed off earlier than usual.
But de-activating PER1 affected eating behaviour, leading to mice wanting to eat when they should be sleeping.
"For a long time, people discounted night eating syndrome as not real," said lead scientist Dr Satchidananda Panda, from the Salk Institute in La Jolla, California.
"These results in mice suggest that it could actually be a genetic basis for the syndrome.
"We really never expected that we would be able to decouple the sleep-wake cycle and the eating cycle, especially with a simple mutation. It opens up a whole lot of future questions about how these cycles are regulated."
When the researchers restricted access to food, offering it to the mice only at normal meal times, they found that even animals with the faulty PER1 gene maintained a normal weight.
Over a ten-week period their weight was no different to that of animals with functioning PER1 genes.
This showed that the weight gain caused by faulty PER1 was entirely due to meal mistiming and not other metabolic factors.
The scientists believe that normally, PER1 and PER2 are kept synchronised and turned on and off at the same time, keeping sleep and eating cycles aligned.
But a mutation in PER1 can break this link, leading to an urge to eat at night.
The research is reported in the journal Cell Reports.