WASHINGTON — President Trump will declare a national emergency as early as Friday to bypass Congress and build his long-promised wall along the nation’s southwestern border even as he agreed to sign a spending package that does not finance it, White House officials said Thursday.

The announcement came just minutes before voting began on the spending measure, which then cleared both houses, ending a two-month war of attrition that closed much of the federal government for 35 days and threatened a second shutdown on Friday. The Senate passed it 83 to 16, and the House followed later in the evening, 300 to 128.

But if he declares a national emergency to access billions of dollars for his wall, Mr. Trump could instigate a constitutional clash over who controls the federal purse and test the bounds of presidential authority in a time of divided government. Democrats and some Republicans instantly condemned the move, with some vowing to challenge it through legislation and lawsuits.

The president’s plan would combine money included in the spending package for fencing along the border with funds that he can divert from other programs using traditional presidential discretion on top of still other money he would tap by declaring an emergency.

Altogether, an administration official said, Mr. Trump would be able to dedicate about $8 billion for barriers, more than the $5.7 billion that Congress refused to give him.

“President Trump will sign the government funding bill, and as he has stated before, he will also take other executive action — including a national emergency — to ensure we stop the national security and humanitarian crisis at the border,” said Sarah Huckabee Sanders, the White House press secretary. “The president is once again delivering on his promise to build the wall, protect the border and secure our great country.”

Mr. Trump’s announcement capped hours of last-minute drama as he came under pressure on Thursday morning to not sign the spending legislation from conservative figures like Laura Ingraham, who denounced it on Twitter as a “monstrosity” and a “Total SCAM!”

A balky president was concerned that signing the measure could impose restraints on his ability to tap other funds and was urged by his chief of staff, Mick Mulvaney, to tell Republican leaders to instead pass a short-term bill to keep the government open while reopening negotiations, according to a Republican briefed on the situation.

Such a move would have unraveled the delicate bipartisan balance favored by Senator Mitch McConnell, Republican of Kentucky and the majority leader, who wanted to move beyond the wall fight. In a telephone conversation on Thursday, Mr. Trump asked Mr. McConnell whether the spending measure included any hidden provisions or “land mines,” and the senator reassured him it did not, according to a person familiar with the call.

Similarly, Kirstjen Nielsen, the homeland security secretary, and White House lawyers told him that he could still move money around, and Ms. Nielsen said that the spending package was actually better than a short-term measure. Mr. McConnell argued that it was a win over Speaker Nancy Pelosi.

Ultimately, Mr. Trump was persuaded to sign the bipartisan spending measure after all, and at least some close to the president doubted that he was ever really wavering and instead was just enjoying the suspense.

But Mr. McConnell chose not to take a chance, rushing to the floor and interrupting a colleague’s speech to announce Mr. Trump’s decision, in effect locking it in before he could change his mind.

In agreeing to end the spending fight for now, however, Mr. Trump essentially started a new one with his vow to declare a national emergency, one that crosses party and ideological lines as liberals and conservatives alike objected to what they called presidential overreach.

Seven Senate Democrats, including four announced or possible presidential candidates, immediately introduced legislation intended to block Mr. Trump from diverting money from disaster relief for the wall. Some House Democrats, including Representative Jerrold Nadler of New York, the chairman of the Judiciary Committee, began endorsing a joint resolution to essentially nullify a national emergency declaration.

A parade of liberal advocacy organizations sent out pre-written statements promising to challenge any such declaration in court as “an outrageous abuse of power” by an “unstable and increasingly autocratic” president, as one of the groups, Public Citizen, put it.

“The president is doing an end run around Congress,” Ms. Pelosi told reporters. She suggested that Mr. Trump was setting a precedent for future Democratic presidents to act on issues like gun control — precisely the scenario that scares Republicans.

“You want to talk about a national emergency, let’s talk about today,” Ms. Pelosi said, reminding Mr. Trump that it was the anniversary of the shooting massacre last February at Marjory Stoneman Douglas High School in Parkland, Fla. Seventeen students and staff members were killed. “That’s a national emergency. Why don’t you declare that emergency, Mr. President? I wish you would.”

Senator Chuck Schumer of New York, the Democratic leader, echoed her criticism and said Congress would defend its prerogatives. “The public was more opposed to the emergency declaration than they were to the wall,” he said in a brief interview. “And they were opposed to the wall.”

About a half-dozen Senate Republicans quickly spoke out against the move, as well. “I don’t think this is a matter that should be declared a national emergency,” said Senator Lisa Murkowski of Alaska. “We as legislators are trying to address the president’s priority. What we’re voting on now is perhaps an imperfect solution, but it’s one we could get consensus on.”

Senator Susan Collins of Maine said such a declaration was “of dubious constitutionality,” while Senator Marco Rubio of Florida called it “a bad idea.” Senator Rand Paul of Kentucky said, “We have a government that has a Constitution that has a division of power, and revenue raising and spending power was given to Congress.”

Still, other Republicans backed Mr. Trump, including an ally, Senator Lindsey Graham of South Carolina, as well as Senator Shelley Moore Capito of West Virginia, who faces re-election next year. “He feels that declaring a national emergency gives him that flexibility that he wants and needs, so I’m supportive of that,” Ms. Capito said of the president.

“For goodness’ sake, we tried for 35 days in December and January, right, to get the Democrats to do what everyone knows needs to happen except Democrats,” said Representative Jim Jordan, Republican of Ohio. “I support the national emergency declaration 100 percent.”

Among the Republicans who privately warned Mr. Trump against an emergency declaration was Mr. McConnell, who expects that House Democrats will pass a resolution terminating it in a form that the Republican leader cannot block from a floor vote. At least five or six Republican senators are likely to vote against the president, making a majority along with the Democrats, and potentially forcing Mr. Trump to veto it.

A person familiar with the discussions said that Mr. McConnell said that he has warned Mr. Trump that he has less than two weeks to try to persuade wavering Republicans to support his national emergency effort, otherwise he will face the prospect of a bipartisan rebuke by Congress.

The spending legislation that passed on Thursday drew strong bipartisan votes in a Congress that has been deeply divided along party lines. In the House, 213 Democrats and 87 Republicans voted yes, meaning that Democrats were unable to pass it by themselves because they lost 19 of their own. Most Republicans, 109 in all, voted no.

Ms. Pelosi, using nearly a dozen pens to sign the final bill, applauded the bipartisan majority, calling it “quite a remarkable show.”

Video

Senator Mitch McConnell, the majority leader, said he will support President Trump’s national emergency declaration. Nancy Pelosi, the speaker of the House, said the move “should be met with great unease.”CreditCreditErin Schaff for The New York Times

In the Senate, most of the 16 no votes were by Republicans, along with four Democratic presidential candidates — Cory Booker of New Jersey, Kirsten Gillibrand of New York, Kamala Harris of California and Elizabeth Warren of Massachusetts.

The package includes the seven remaining bills to keep the remainder of the government open through the rest of the fiscal year at the end of September. House and Senate negotiators unveiled the 1,159-page bill on Wednesday just before midnight, leaving little time for lawmakers to actually digest its contents.

The measure prohibits construction in certain areas along the Rio Grande Valley and includes a provision, pushed by Representative Henry Cuellar, Democrat of Texas, granting communities on the border time to weigh in on the location and design of the fencing.

The bill also prohibits funds from being used to keep lawmakers from visiting and inspecting Department of Homeland Security detention centers, following a number of highly publicized instances where Democratic lawmakers tried to visit detention centers and were turned away.

Lawmakers were drawn by the other six parts of the spending package that finance a number of agencies, including the Internal Revenue Service, which is in the middle of tax-filing season, and the Commerce Department. Allocations include $77 million to address the opioid epidemic, as well as money for natural disasters, including nearly $4 billion to wild-land fire programs and $12.6 billion for the Federal Emergency Management Agency’s disaster relief fund.

The package also negates an executive order that Mr. Trump signed to freeze pay for federal civilian workers, and instead extends a 1.9 percent pay increase. Vice President Mike Pence, cabinet officials and other high-level political appointees will receive raises of about $10,000 a year that had been frozen during the shutdown.

Negotiators failed to resolve other matters, including back pay for federal contractors caught in the middle of the shutdown and an extension of the Violence Against Women Act, which expires Friday — although grants under the act are funded in the spending bill.

Mr. Trump’s aides have told him he can add to the $1.375 billion by reallocating money from other related programs using his traditional discretion but a national emergency would allow him to access even more money. Exactly how that would work was still being debated; there were multiple drafts of the emergency order circulating within the government this week.

A Defense Department official said one likely scenario would be to divert up to $2.5 billion in counternarcotics funds to the Army Corps of Engineers. The administration would then transfer existing federal land along the border to the Defense Department and acquire or condemn private land.

The Army Corps could then go ahead and build a wall or other barrier to secure the Defense Department’s property using the newly diverted funds. Dozens of miles of barrier could be built this way, according to the official, who asked not to be identified discussing alternatives before the White House was ready to disclose a specific plan.

Legal experts have said Mr. Trump can muster serious arguments that he can declare a national emergency under current law, but it would almost surely prompt a court challenge from critics arguing that he is usurping two centuries of congressional control over spending.

Under Article I of the Constitution, Congress has the power to appropriate funds. “No money shall be drawn from the Treasury, but in consequence of appropriations made by law,” it says.

But Congress has passed laws in the past providing presidents with authority in national emergencies, laws that remain on the books. Indeed, 31 national emergencies declared by Mr. Trump and his predecessors remain active.

Legal challenges could ultimately lead to a confrontation at the Supreme Court. The court is led by a five-member conservative majority, but it has shown skepticism of presidential excesses in recent years, reining in both President George W. Bush and President Barack Obama when the justices concluded they had overstepped their authority.