The U.S. House of Representatives voted early Friday in favor of blocking the federal government from interfering with states that permit the use of medical marijuana.

The somewhat surprising 219-189 vote came as the House, which is controlled by the Republican Party, debated a bill funding the Justice Department's budget.

The amendment by Republican representative Dana Rohrabacher of California—the first U.S. state to legalize —came as almost half the states have legalized marijuana for medical uses, such as improving the appetites of cancer patients undergoing chemotherapy.

"Public opinion is shifting," Rohrabacher said, noting a recent Pew Research Center study that found 61 percent of Republicans support medical marijuana. The numbers are higher for independents and Democrats.

Opponents say marijuana is regulated too loosely by the states and harms the brain.

The measure now heads to the Democratic Senate.