An armed suspect was shot and killed by police inside a New Jersey library Friday after he struggled against police officers who attempted to detain him and then charged at them with a knife, authorities said.

Two police officers spotted the suspect, who had a warrant for his arrest, walking into the Lyndhurst Public Library on Valley Brook Avenue at about 2:30 p.m., according to Lyndhurst Police Chief James O'Connor.

The officers approached the suspect, identified by prosecutors as Kevin K. Allen, 36 -- who also has an alias of Kevin Hall -- on the top floor of the library and attempted to detain him, according to O'Connor.

Allen struggled and continued to fight even as the officers attempted to use pepper spray and a baton, O'Connor said. The suspect then pulled a knife out and rushed at the officers, and that's when they fired.

Witness Jayne Thorne was leaving the library when the shooting happened.

"Not even a minute I was outside the library, and all of a sudden two plainclothes men and one cop comes running by," she said. "I rushed into the library, and the next thing I know they were dragging someone out on a stretcher."

The chief said the shooting was justified and the officers had no choice.

"There was nothing else they couldh ave done," he said.

The two officers were not injured but were transported to the hospital for trauma, according to O'Connor.

There were people inside the library, and several witnessed the shooting, including a child, according to authorities. No bystanders were hurt, which neighbors deemed lucky considering how many children go to the library on a Friday afternoon.

"If that would have happened a half-hour earlier or later, there would have been kids doing projects, or just weekend stuff, and God forbid if the gunman escaped or held peoploe hostage," said neighbor Robert O'Dell. "When you're a parent, that goes through your mind."

No one answered the phone at the library.