Narayana Upanishad listed as 18th out of 108 Upanishads in Muktika Upanishad which is discussion between Rama and Anjaneya. In 4th chapter of Narayana Upanishad, it’s said as Narayana as Devaki putra, means Krishna, this event happened in Mahabharata. As Ramayana happened before Mahabharata, how to relate both events?
This relates to the Hindu concept of time and creation. World is destroyed and recreated in cycles of chaturyugi, manvantar, kalpa &c. Apart from that, there are also parallel universes (multiverse) in which Dharmashastras are revealed from time to time untill they're lost again. So, Valmiki had written Ramayan before Lord Ram ever incarnated on Earth. Also, Vasisth told the the gist of Gita to a 16 year old Ram as it would have been recited to Arjun by Krishna, aeons before incarnation of Krishna. In simple terms, per Vedic theology, the sastrakaras are Trikalagya. Accordingly, your problem may be explained as such.
- Sriyogvasisthmaharamayan Sthiti Prakaran (cyclic time)
- Sriyogvasisthmaharamayan Leelopakhyan (parallel universe)
- Sriyogvasisthmaharamayan Nirvana Uttarardh (Bhavishya Geeta)