The US Geological Survey said the tremor occured around 5 miles off the coast inbetween the southeastern city of Taitung and Hualien, further north.
The quake shook buildings in the capital, Taipei, at the far north of the island. Taiwan’s weather bureau said there were no immediate reports of damage.
Around 14m people reported feeling shaking as far as 400km away from the epicentre. The quake hit around 1.40am local time.
Several locals told the European-Mediterranean Seismological Centre it was the longest quake they had felt for some time.
By one account the shaking went on for 30 seconds and was followed immediately by an aftershock.
The US Tsunami Warning System said it did not issue a warning after the earthquake hit.
Hualien and Taitung are both mountainous, relatively sparsely populated areas.
Taiwan lies near the junction of two tectonic plates and is prone to earthquakes.
At least 17 people were killed when a magnitude 6.4 earthquake struck off the coast near Hualien in 2018.
More than 100 people were killed in a quake in southern Taiwan in 2016, while a 7.3 magnitude quake killed more than 2,000 people in 1999.