WASHINGTON — The factional rancor threatening Republicans heading into the midterm elections this fall erupted into the open on Friday when a slugfest among moderates, hard-line conservatives and House leaders over immigration and welfare policy sank the party’s multiyear farm bill.

The twice-a-decade measure — which would have imposed strict new work requirements on food aid recipients while maintaining farm subsidies important to rural lawmakers — failed on a 213-to-198 vote. It was a rebuke of Speaker Paul D. Ryan by a key bloc of conservatives over his refusal to schedule an immediate vote on a restrictive immigration bill sponsored by the chairman of the House Judiciary Committee.

Republican moderates, for their part, were moving in the opposite direction, shrugging off the pleas of their leaders as they worked toward forcing votes on legislation to protect from deportation young immigrants brought to the country illegally as children.

The fights were striking, not only because of their intensity but also because of the participants. Capitol Hill has grown used to altercations between Republican leaders and their adamant right flank — showdowns that have shut down the government and edged the government toward defaulting on its debt. But in past fights, the party’s moderates have proved compliant.