President Trump has repeated vows to stop U.S. aid to Central American countries that do not disband the caravan, estimated to contain more than 3,000 people. (Jabin Botsford/The Washington Post)

Facing a sharp increase in unauthorized immigration, President Trump on Thursday lashed out at Democrats and the leaders of Latin American nations, seeking to deflect blame and mitigate political damage by riling up his base just weeks before the midterm elections.

The president’s mounting frustration was evident in a series of tweets in which he threatened to summon the military to guard the southern border, cut off aid to Central American nations and upend a recent trade deal with Mexico if those governments fail to stop a large caravan of migrants from Honduras making its way toward the United States.

Trump likely lacks the legal authority to carry out all of those drastic measures without congressional approval, but the threats highlighted the potentially perilous political implications for the White House as voters prepare to go to the polls with Republican control of Congress in the balance. In recent campaign rallies, Trump has sought to rally conservatives by asserting falsely that Democrats are in favor of “open borders” and that they favor increased crime.

“All Democrats fault for weak laws!” Trump wrote in one tweet, adding later: “Can you believe this, and what Democrats are allowing to be done to our Country?” The second tweet included a video clip of unknown origin that showed men, speaking Spanish, handing out slips of paper to people in lines carrying bags. The footage was identical to a clip posted by Rep. Matt Gaetz (R-Fla.), who claimed it showed cash payments to Hondurans to join the caravan and suggested, without evidence, that the money came from a Democratic megadonor.

Republicans are facing a difficult election on Nov. 6, with polls showing Democrats holding an advantage on the general ballot and prognosticators favoring them to win control of the House. As pressure mounts, nerves have begun to fray inside the White House, with national security adviser John Bolton and Chief of Staff John F. Kelly engaging in a shouting match over immigration just outside the Oval Office on Thursday, according to White House aides.

Bolton had accused Homeland Security Secretary Kirstjen Nielsen of not doing enough to stem the surge in border crossings, and Kelly, who preceded Nielsen in the homeland security job and handpicked her as his successor, came to her defense, aides said. They described the argument as profane and loud enough to draw stares from others in the West Wing.

A White House official said Nielsen came to Bolton’s office after the fight and the two had a positive conversation about her department and the border.

While some GOP leaders in the House had initially hoped to focus their election message on the economy, Trump has continued to highlight immigration, confident that his hard-line views on enforcement will motivate his base and carry significant crossover appeal to independent and moderate voters who helped him win in 2016.

Many Republican candidates, especially in Senate races in states Trump won in 2016, have modeled attack ads on Trump’s rhetoric, aiming to raise public fears over the dangers of illegal immigration. Recent polls show that voters in both parties rank immigration among the top issues that will affect their choices in next month’s elections.

“The border is a big issue for Republican base voters who are already energized after Kavanaugh,” said Chris Wilson, a ­Republican pollster working on battleground races, referring to the contentious Supreme Court confirmation battle over Justice Brett M. Kavanaugh. “A reminder that illegal immigration is a real problem and that we just don’t know who is entering our country can only boost Republican interest.”

Democrats dismissed Trump’s accusations as a sign of weakness from a president desperate to escape electoral consequences for failing to make good on his signature campaign promise to reduce illegal immigration.

Border Patrol agents arrested more than 16,000 immigrant families at the U.S.-Mexico border in September, the highest total on record and an increase of 80 percent since July, when Trump reversed his administration’s policy of separating immigrant children from their parents after a public outcry.

In all, more than 107,000 family members were taken into custody in fiscal 2018, obliterating the previous high of just under 78,000 in 2016.

“It’s pretty clear that the things this administration has attempted to address the situation at the border are failing,” said Cecilia Muñoz, an analyst at the think tank New America who served as a White House domestic policy adviser in the Obama administration. “As is typical, this president is trying to lay the blame elsewhere.”

The immigration increase this year, after a sharp drop in the first six months of his presidency, has complicated Trump’s immigration message. In response, he has exhorted aides to enact more extreme policies, including making it more difficult for immigrants to seek asylum protections and measures aimed at increasing criminal prosecutions of unauthorized border-crossers.

The White House is also considering a new policy, known as “binary choice,” which would detain migrant families together and give parents a choice — stay in immigration jail with their child for months or years as their asylum case proceeds, or allow their child to be assigned to a government shelter while a relative or guardian can apply to gain custody.

In his tweets Thursday, Trump aimed to ramp up pressure on foreign governments.

“In addition to stopping all payments to these countries, which seem to have almost no control over their population, I must, in the strongest of terms, ask Mexico to stop this onslaught — and if unable to do so I will call up the U.S. Military and CLOSE OUR SOUTHERN BORDER!” Trump said in one tweet.

In another, he suggested that the “onslaught” of immigrants could undermine a recently announced reworked trade deal with Mexico and Canada, writing that immigration is “far more important to me, as President, than Trade.”

The new deal, which replaces the North American Free Trade Agreement, has yet to be signed by the three countries. Congress is not expected to ratify it before next year.

In recent years, Mexico has detained and deported far more Central Americans attempting to cross Mexico on the way to the United States. Still, Mexico’s southern border with Guatemala remains relatively easy for migrants to cross.

In the wake of Trump’s warnings, the Mexican government signaled that it would take a more restrictive stance on the caravan of migrants this time. Planeloads of federal police have arrived at Mexico’s southern border and are positioned at crossing points. The Mexican Foreign Ministry requested assistance Thursday from the U.N. Refugee Agency in processing Central American refu­gee claims.

The incoming government of Mexican President-elect Andrés Manuel López Obrador, who takes office in December, campaigned on a softer approach to migrants, saying that protecting their human rights should be a top priority and that they shouldn’t be treated like criminals.

But in response to the latest caravan from Honduras, said to number up to 4,000 people, top officials on López Obrador’s transition team have suggested that they would move to block such large groups of people.

“Mexico can’t say that any caravan can pass to the United States,” Marcelo Ebrard, the incoming foreign minister, said Thursday on Mexican radio.

Mexican government leaders are “trying to perform a very difficult balancing act — on one hand with Trump watching and on the other hand with the Mexican population watching,” said Carlos Bravo Regidor, a professor at the Center for Research and Teaching in Economics in Mexico City.

Immigration is likely to be discussed Friday when Secretary of State Mike Pompeo is scheduled to meet with outgoing Mexican President Enrique Peña Nieto.

In Washington, no matter how the midterm elections play out, the issue could come to a head in December.

Trump had threatened to force a partial government shutdown in the fall to force Congress to authorize billions of dollars for his proposed border wall. Instead, he signed a stopgap spending measure, setting up a potential fight over the wall in the congressional session between the election and the new Congress.

“Any purely enforcement-oriented solution will only move the needle in the short term,” said Leon Fresco, an immigration attorney who served as an aide to Senate Minority Leader Charles E. Schumer (D-N.Y.). “Over the long-term, in order to solve that, he has to either be realistic about a bi­partisan solution and work with Congress — or continue to demagogue it as a political issue, but that’s not going to solve it.”

Partlow reported from Esquipulas, Guatemala. Josh Dawsey, Sean Sullivan, Alex Horton and Nick Miroff in Washington and Kevin Sieff in Mexico City contributed to this report.