Parliament has passed a new bill that will make it a criminal offence to send harmful messages or post damaging images or comments online.
The wide-reaching law is designed to crack down on cyber-bullying, with serious offences attracting up to two years in jail or fines of up to $50,000.
The Harmful Digital Communications Bill passed its third and final reading on Tuesday night by 116 votes to five, with ACT's David Seymour and four Green MPs voting against it.
It introduces a range of measures, including setting up an agency to investigate and address damaging electronic communications spread through emails, texts and social media posts.
The law was drafted after a case in Auckland in which teenage boys boasted online about sex with drunk and underage girls, leading to the girls being further victimised.
New Zealand First deputy leader Tracey Martin told Parliament that people needed to be protected from anonymous and vicious comments made on apps such as Yik Yak.
She said that app had particularly nasty effects in secondary schools.
Ms Martin said people got hooked into Yik Yak by becoming popular after making five or more really horrible comments.
National MP Chris Bishop said traditional schoolyard bullying had moved online, with one in five adolescents saying they had experienced harmful communications on the internet.
Gareth Hughes, who was one of the four Green MPs to make the rare step of casting a vote separate from his party, said the four were concerned it could have a chilling effect on freedom of speech.
"Ultimately this bill is over broad and risks limiting our freedom of expression and the important role of the media in our democracy."
It will now be an offence to send a message or post material online that is intended to cause harm, and does so.
It will also be an offence to incite suicide.
Labour MP Jacinda Ardern said Labour felt backed into a corner.
"Yes there are elements that we think were worthy of discussion but there are elements that were deeply worrying for us," she said.
National MP Scott Simpson said Parliament was trying to grapple with the downsides of advancing technologies.
He said the bill was an attempt to remedy the harm.
Communications Minister Amy Adams has agreed to review the law if any problems arise.
An internet users lobby group says the agency that will oversee the new laws will be crucial to making sure the regulations work properly.
Internet New Zealand spokesman Andrew Cushen said the agency had to strike a balance between protection and free speech.
He told Morning Report the agency would be a positive force if it was educating people about how to communicate online as well as monitoring behaviour.