Nine other workers were injured, four of them seriously, Uttar Pradesh state official Naresh Mathur said.
The factory was in a converted house in the industrial town of Sahibabad, and had a narrow stairway into the basement where the workers slept.
A dozen fire engines doused the flames after battling them for several hours. The cause of the fire is being investigated.
Factory fires are common in India, where safety enforcement is lax.