The bill ending the religious exemption for vaccinations comes as public health officials nationally and in New York are grappling with a measles outbreak that has grown to more than a thousand confirmed cases, many of them in the state.

Gov. Andrew Cuomo has signed the bill and it takes effect immediately.

“I believe we need to vaccine as many people as possible,” said Sen. David Carlucci. “It seems like when we eradicated the measles in 2000, we eradicated the memory of the measles.”

A Siena College poll this week found more than 80 percent of voters back ending the exemption. But the bill passed narrowly in the state Assembly, by only two votes, amid a noisy protest from dozens of anti-vaccine advocates who believe vaccines can cause health problems.

“New York state constitutional freedoms are not up for grabs by the Assembly,” said Stephanie Maharris, one of the protesters. “This is a violation of the most basic principles of the United States of America.”

The bill was narrowly approved by the Assembly Health Committee as well, and one lawmaker, Assemblyman Nader Sayegh, changed his vote after it became apparent the measure did not have the support. Sayegh, a Democrat from Yonkers, voted against the bill on the floor.

Multiple Democrats voted against the bill, including Health Committee Chairman Dick Gottfried, who had voted to advance the bill out of his committee.

Assemblyman Tom Abinanti was skeptical the religious exemption repeal would help.

“There’s been no showing of any connection between the religious exemption for vaccines for some 20,000 people in the state of New York and the spike in measles,” Abinanti said.

Nearly all public health experts agree vaccines are necessary for healthy people to receive herd immunity and protect people with cancer and other illnesses who are at risk for disease.

Supporters also said the First Amendment does not apply — comparing the issue to shouting fire in a crowded theater when there is no fire.

“Science is what should determine our health policy,” said Assemblyman Jeff Dinowitz, one of the bill’s main sponsors. “Medical experts should determine our health policy and the people who are opposed to it have the right to think whatever they want, but they are a tiny, tiny, minority in New York.”

After the Assembly passed the bill, protesters yelled and swore at the lawmakers from the gallery, creating a chaotic scene. Dinowitz shrugged off the protest.

“I believe they have come to decisions they’ve gotten from social media, the internet, but not based on science,” he said.