A sprawling and subsidy-packed farm bill that also would have tightened food stamp rules failed in the House on Friday, in a dramatic defeat for GOP leaders who faced an uprising from conservatives over inaction on immigration.

The farm bill, which was voted down 213-198, had been hanging in the balance as the conservative House Freedom Caucus threatened to withhold support without an agreement on an immigration bill vote.

The GOP leadership, including House Speaker Paul Ryan, R-Wis., Majority Leader Kevin McCarthy, R-Calif., and Whip Steve Scalise, R-La., were seen in intense discussions with the conservative lawmakers in the minutes before the vote.

At least 30 Republicans voted against the bill, while all Democratis opposed it -- enough to doom the legislation. Ryan then took steps to call for a revote in the future, but it's not clear when the measure might be revived.

“We experienced a setback today after a streak of victories all week," said House Agriculture Committee Chairman Michael Conaway, R-Texas. "We may be down, but we are not out."

Conway predicted the House will "deliver a strong, new farm bill on time as the president of the United States has called on us to do."

Before the farm bill failed, the freedom caucus had called for a vote on a conservative bill that would boost border security and support the construction of a wall, among other changes. GOP leaders had offered for the House to consider the measure in June in exchange for support on the farm package, a deal the conservatives rejected.

A spokesman for Freedom Caucus Chairman Mark Meadows, R-NC., said Friday he opposed the bill because not enough progress has been made toward a solution on immigration. The spokesman added Meadows look forward to continuing discussions in the coming days.

“We want to make sure we get the right thing done on immigration,” Rep. Jim Jordan, R-Ohio, told “Fox News @ Night” on Thursday.

Leadership has been hearing grief in stereo from both conservatives and moderates – who are insisting the House address protections for “DACA” recipients, young illegal immigrants who came to the U.S. as children. President Trump stripped those protections, calling on Congress to act. But the matter has been held up in court, and some moderate Republicans are trying to light a fire under House leadership to deal with the issue sooner rather than later.

All this came as Congress worked to engineer a passable farm bill, a twice-a-decade effort that often turns into an epic balancing act to address the wishes of farm-state Republicans and Democrats supporting food stamps.

On top of the complicating factor of the immigration fight, the House on Thursday dealt with an effort to crack down on the government’s controversial sugar subsidy program. In a decisive 278-137 vote, the House rejected a bid by Rep. Virginia Foxx, R-N.C., to significantly weaken the program and invite more foreign competition.

But GOP leaders are also pushing to tighten work and job training requirements for food stamps, as part of the bill.

Currently, adults 18-59 are required to work part-time to receive food stamps, officially called the Supplemental Nutrition Assistance Program, or agree to accept a job if they're offered one. Stricter rules apply to able-bodied adults 18-49, who are subject to a three-month limit of benefits unless they meet a work or job training requirement of 80 hours per month. Under the new bill, the tougher requirement would be expanded to apply to all adults on SNAP, with exceptions for of seniors, pregnant women, caretakers of children under the age of 6, or people with disabilities.

But with Democrats opposing those changes, the situation gave the House Freedom Caucus leverage to make their immigration demands.

Before the vote, Meadows said "the time is now" to deal with immigration and that the farm bill doesn't face a pressing deadline. He said farmers "want us to deal with immigration and the farm bill both."

Meadows and other Freedom Caucus members met with House leaders into the evening Thursday to try to resolve their disputes.

The uprising is also a symptom of the shaky leadership situation, with Ryan retiring. The balancing act is a test for his potential replacement candidates, McCarthy and Scalise.

Ryan had tried to separate the farm bill from immigration issues, but so far has not been able to do so.

Meanwhile, President Trump prodded lawmakers to act with an overnight tweet saying that on Friday, “the House will vote on a strong Farm Bill, which includes work requirements. We must support our Nation’s great farmers!”

White House deputy press secretary Lindsay Walters said Friday the president is “disappointed” in the vote and “hopes the House can resolve any remaining issues in order to achieve strong work requirements and support our nation’s agricultural community.”

Fox News’ Judson Berger, Serafin Gomez and Mike Emanuel and The Associated Press contributed to this report.