Real raced into a two-goal lead after only eight minutes, with Mikel Merino opening the scoring with a close-range finish from a corner.
Five minutes later David Lopez handled in the area and, after a VAR review, a penalty was awarded which Willian Jose converted.
But Espanyol clawed their way back into the game in the 32nd minute when Naldo headed in from a corner.
Then, in first-half stoppage time, Real centre-half Diego Llorente attempted to clear the ball after it came back off the crossbar but only succeeded in bundling it into his own net.
But just after the hour mark, Willian Jose tucked in a cross from Merino to secure the three points.
Real finished the game with 10 men after Merino was sent off for a second booking 15 minutes from full-time, but they held on for a victory that moves them above their opponents and within three points of the top six.