The House on Thursday passed a bill to raise the U.S. debt ceiling and set budget levels for two years, taking a step toward avoiding calamity that threatens to disrupt the economy.

The Democratic-held chamber approved the measure in a 284-149 vote. Democrats voted 219-16 in favor of the bill. Republicans opposed it by a 132-65 margin, even after President Donald Trump urged the House GOP to support it earlier Thursday. The one independent in the House also voted against the bill.

It sets discretionary spending at about $1.37 trillion in fiscal 2020 and slightly higher in fiscal 2021. The agreement suspends the U.S. borrowing limit for two years.

The House vote sends the measure to the Senate, which is expected to pass it in the coming days and send it to Trump's desk. The president is expected to sign it.

Congress hoped to lift the debt ceiling before September, when Treasury Secretary Steven Mnuchin warned the U.S. could run out of ways to pay its bills. That prospect risked debt default, which would rattle the U.S. and global economies.

Passing the bill will also avoid sequester, automatic across-the-board spending cuts set to take place next year. Congress still has to pass separate appropriations bills to avoid a government shutdown before funding lapses at the end of September.

Both Democratic and Republican congressional leaders secured what they can point to as wins in the deal. It includes rough parity in spending hikes for defense and domestic, nondefense programs.

But some lawmakers and advocates for federal budget restraint slammed the deal as annual budget deficits are projected to top $1 trillion in the coming years. Republican tax cuts and spending increases backed by both parties have contributed to the widening deficits.

Earlier Thursday, Trump urged House Republicans to back the legislation. Some conservative GOP members criticized the deal in part because it permanently ends sequester.

"House Republicans should support the TWO YEAR BUDGET AGREEMENT which greatly helps our Military and our Vets. I am totally with you!" the president tweeted.

Trump tweet: House Republicans should support the TWO YEAR BUDGET AGREEMENT which greatly helps our Military and our Vets. I am totally with you!

His push did not appear to sway the hard-line conservative House Freedom Caucus. In an opinion piece in USA Today published Thursday afternoon, the group criticized the agreement.

The caucus called it "deeply flawed" and said Congress should work to "hammer out a budget agreement that responsibly cuts spending and sets the country on a track to fiscal solvency."

Subscribe to CNBC on YouTube.