A New Jersey police officer arrested after suffering a heroin overdose in his patrol car as he worked a midnight shift has struck a plea deal with prosecutors that will cost him his job, according to reports.

Prosecutors said Franklin Township cop Matthew Ellery had to be revived by a colleague who found him unconscious and administered an antidote commonly known as Narcan, the Newark Star-Ledger reported.

Ellery, 29, was charged with heroin possession and driving while intoxicated after the April 7 incident and on Friday pleaded guilty in Somerset County Superior Court.

Matthew Ellery, 29, pleaded guilty Friday. (Somerset Couty Prosecutor's Office)

FLORIDA SHERIFF DEPUTY REVIVED WITH NARCAN, 3 OTHERS RECOVERING AFTER EXPOSURE TO DRUGS

In exchange, Ellery agreed to enroll in a drug treatment program and forfeit his job, according to TapInto Franklin, a news website. It added that he could avoid prison if he completes the program.

The cop who found Ellery had been dispatched to the officer's last location after a dispatcher tried to reach Ellery on the radio and couldn’t, the website reported.

CLICK HERE TO GET THE FOX NEWS APP

“Matt Ellery is a wonderful young man who has a problem and we’re trying to do what’s needed so he resolves his problem and moves on with his life,” defense attorney Steven Altman told the Star-Ledger.

The paper reported that after the news broke last month about Ellery’s arrest, the Franklin Township Police Department's two top cops abruptly announced their retirements.