This particular quote is not a direct quote from Shakespeare. The word "nightmare" only appears once in Shakespeare's plays and it is not in a context anything like this quote. (This is according to a couple of sites where you can search all of Shakespeare's works at once.)
I think that you may be thinking of a famous line that is from Shakespeare and has a somewhat similar meaning. It is from Hamlet's "to be or not to be" soliloquy in Act III, Scene 1 of Hamlet. In that passage, Hamlet says
To sleep- perchance to dream: ay, there's the rub!
For in that sleep of death what dreams may come
When we have shuffled off this mortal coil
That sort of means the same thing -- if you dream, you may have nightmares.