Insomnia and depression are very closely related. Sometimes it's hard to determine what came first. Is it insomnia causing your depression, or is it depression leading to insomnia? Now an extensive history taken by your physician can sometimes help determine if your insomnia came first. Taking an extensive history by your physician may help determine if your depression came first. Treating the underlying depression is needed to help treat your insomnia, because if you try treating the insomnia with the use of sleep aids or other techniques and you haven't treated your depression, you will have a harder time treating your insomnia.
So there are chemical imbalances that are seen in depression which might be also contributing to insomnia at night. That's why it is important to treat your depression before treating insomnia. So treating your depression in order to alleviate insomnia might include the use of medications such as antidepressants, but also talk therapy or seeing a psychologist, which may help avoid the use of medications and might help you learn to cope with your depression a little bit better without the use of medication.