Check the code numbers in memory
Read here: Ford Ranger - Testing EEC-IV Equipped Engines
This will narrow down what to check, it reads like you have a bad connection somewhere, code number will tell you what sensor is going off and on.
Over the years connections can get moisture inside and corrode, simply unplugging them and having a look inside can tell you, and plugging them back in makes cleaner connection if it was just a little dirty.
Check TPS(throttle position sensor) connector
MAF sensor connector, also clean MAF sensor.
Bogging and dirty MAF sensor are common with 4.0l, read here: Ford Ranger Mass Air Flow Cleaning, Removal and Installation
Other thing could be Fuel Pump Relay or fuel pump itself.
Fuel pump relay is controlled by the computer, when engine is running it should stay closed, when closed power is being sent to fuel pump AND computer, computer monitors if power is on or off and will turn on CEL if it goes off when it should be on.
So if fuel pump relay is opening then closing again fuel pump will go off, engine will stumble, and stall if it is off long enough, and CEL would come on.
If fuel pump itself was failing then when it goes off, engine will stumble and misfire turning on CEL.