WASHINGTON — Immigration is once again dividing Republicans — this time, in the House of Representatives where a battle between conservative and moderate factions resulted in the defeat on Friday of a farm bill supported by party leadership.

It's an intraparty feud between disparate factions that has ramifications for GOP candidates in this fall's midterm elections.

The battle over border security and the Deferred Action for Childhood Arrivals (DACA) program is also posing another challenge for outgoing Speaker Paul Ryan, whose members are increasingly willing to defy him, and for the man who wants to succeed him — Rep. Kevin McCarthy of California. Neither has been able to bring the party together on an issue that has divided it for years.

The discontent played out on the House floor Friday morning when the conservative House Freedom Caucus withheld their votes for the farm bill, joining Democrats who opposed its cuts to food programs for the poor. The combination resulted in the bill’s failure and left Republican leaders scrambling to find a path forward on a bill that is one of their remaining legislative priorities for the year.

The latest flare-up began last week when a group of moderate Republicans, now totaling 20, started a process that would force a vote on a series of immigration bills, known as a discharge petition. They want to bring to the floor a series of bills, including one that would offer a path to citizenship for accompanied childhood arrivals, known as Dreamers, alongside funding for border security — a proposal that would most likely gain the support of many Democrats.

Leadership, worried about a rebellion among conservatives who adamantly oppose the move and trying to stave off passage of a bill that would likely pass with mostly Democratic support, has been working to come up with a solution that would appease both the conservatives and the moderates.

GOP leaders offered the conservatives a vote in June on a hard-line piece of legislation that would fund President Donald Trump’s proposed border wall, drastically reduce legal immigration and provide renewable permits for Dreamers, even though such a bill does not have enough votes to pass the House or the Senate.

But conservatives weren’t pleased with the offer and voted against the farm bill, which sets agriculture trade and subsidy policy for the country’s farmers, and would reform food-aid programs by instituting work requirements, a long-held Republican wish.

"This is all the more disappointing because we offered the vote these members were looking for, but they still chose to take the bill down,” Doug Andres, spokesman for Ryan, said after the vote.

But while the discussions take place on Capitol Hill, the issue is bleeding into midterm politics.

The moderate Republicans forcing the vote on immigration are from swing districts and among the most endangered incumbents in November, including Carlos Curbelo of Florida, Will Hurd of Texas and Jeff Denham of California. The three represent moderate districts with large Hispanic populations, and providing permanent relief to Dreamers could help them win re-election in November.

But leadership, specifically McCarthy, who needs the support of conservatives to win election as speaker (should the the party retain its majority), is arguing that it’s not good politics to take up an immigration bill that would be supported by most Democrats.

During a closed-door meeting with the Republican conference Thursday, McCarthy told members that the immigration discharge petition would only embolden House Minority Leader Nancy Pelosi, D-Calif., and turn control of House business over to her, according to three GOP House sources. McCarthy also told the conference this effort would depress the Republican base in the midterm elections.

But the defeat of the farm bill over immigration has upped the stakes for Republicans. Rep. Patrick McHenry of North Carolina, a member of leadership who helps ensure the party has enough votes to pass legislation, says that Republicans who had not yet committed to signing the discharge petition will now be more apt to do so. But he said Republicans will now inevitably have a reckoning on immigration.

“I think Republicans in the majority need to resolve our vote on immigration or a series of votes and come to terms with that,” McHenry told reporters. “We need to fix this crisis of immigration, we need real border security, and we need to come to terms with what is our plan as Republicans.”