A U.S. federal government shutdown was averted at the last minute on Saturday after the Republican-led House of Representatives passed a 45-day stopgap funding bill.

A shutdown had seemed almost certain, but House Speaker Kevin McCarthy unexpectedly pivoted to accept Democratic support to fund government agencies through Nov. 17.

The bill passed the House by a bipartisan vote of 335-91. The new package did not include steep spending cuts that Republicans had sought and additional aid for Ukraine that Democrats, who control the Senate, were pursuing.

The bill was later endorsed 88-9 in a vote in the Senate and signed into law by Democratic President Joe Biden just before the midnight deadline.

Senate Majority Leader Chuck Schumer gives two thumbs up in Washington on Sept. 30, 2023. (AP/Kyodo)

"Tonight, bipartisan majorities in the House and Senate voted to keep the government open, preventing an unnecessary crisis that would have inflicted needless pain on millions of hardworking Americans," Biden said in a statement. "But I want to be clear: we should never have been in this position in the first place."

The shutdown could have furloughed hundreds of thousands of federal workers, with the White House earlier this week estimating more than 2 million military personnel would have gone without pay. It could have also resulted in the closures of national parks as well as government-funded facilities and services.

U.S. Treasury Secretary Janet Yellen on Friday warned that "the failure of House Republicans to act responsibly would hurt American families and cause economic headwinds that could undermine the progress we're making," when she delivered a speech on the Biden administration's economic agenda.

The congressional deadlock was largely due to infighting among Republicans who control the House with a slim majority. It remains unclear if the federal government can secure funding after 45 days without a hitch, given that Republican hardliners are now furious with McCarthy.

One major point of contention has been whether to spend more to support Ukraine, which remains under invasion attack by Russian forces.

After more than 19 months since Russia invaded Ukraine, some far-right Republicans, particularly those who are supportive of former President Donald Trump, have sought steep spending cuts and voiced resistance to additional aid for the war-torn country.

The country's federal fiscal year runs from Oct. 1 to Sept. 30. To avert a lapse in funding, budget legislation must be approved by both houses of Congress and signed by the president before the start of each fiscal year.

A federal government shutdown is far from rare in the United States. The last one, the 21st in five decades, spanned 35 days from December 2018 to January 2019.

It was the longest in U.S. history and occurred because of disagreements over then President Trump's demands to fund the construction of a wall along the border with Mexico to prevent asylum seekers and immigrants from crossing into the United States.

Related coverage:

U.S. president's son indicted on federal counts of firearm offenses

Trump surrenders at jail, becomes 1st ex-U.S. president to have mug shot

Biden trying to meet North Korean leader without preconditions: official