One of the deadliest gas accidents in recent memory occurred in 2010 in San Bruno, Calif., when an electrical failure sent high-pressure gas into an old and faulty pipeline. The ensuing fireball killed eight people and destroyed 38 houses.

Since then, utility companies have been pushed to improve safety and update old infrastructure, an expensive proposition.

In Massachusetts, where some pipelines are a century old, state lawmakers passed a law in 2016 that required utility companies to fix the most significant gas leaks. In April, Mr. Bryant, the leader of Columbia Gas of Massachusetts, requested permission to increase rates to pay for upgrades to aging infrastructure. On the day of the explosions, the company had announced that it would be upgrading gas lines in Merrimack Valley, which includes the three municipalities that were hit with fires.

In old mill towns, like Lawrence, some pipes predate the advent of natural gas and were created for another substance, called coal gas. Replacing old pipe with new can cost as much as $1.9 million a mile, said Nathan Phillips, a Boston University professor who has been an environmental activist working on the issue of gas leaks.

About a third of the Massachusetts gas pipeline system is leak-prone and old, Mr. Phillips said.

The fallout from the explosions was overwhelming: Thousands of people were being urged to stay out of their homes, which were deemed unsafe, and others were allowed to return but told not to turn their natural gas back on. As many as 80 buildings had been burned. And some 400 people had wound up sleeping in five shelters that were hastily opened overnight; some of them were likely to be there again on Friday night and, with no promises from officials about when this would be over, maybe for more nights.

Many residents who had abruptly fled their homes without their medications or pets were calling town officials seeking escorts to return home to retrieve essentials. Others who had stayed in their homes were coping with a different problem: no electricity. More than 17,000 customers were without power by Friday afternoon.