He said this while teaching in the synagogue in Capernaum.

McGarvey tells that the synagogue in Capernaum was discovered by Col. Wilson whom he quotes as saying of it:

“It was not without a certain strange feeling that, on turning over one of the blocks (in the ruins), we found the pot of manna engraved on its face, and remembered the words, “I am that bread of life. Your fathers did eat manna in the wilderness, and are dead”.” (TFG 390)

“in the synagogue” is actually not a precise translation. John left out the article “the” so that this verse should read “in synagogue” which would correspond to our modern day “in church.” Jesus was not necessarily in the building; but He was at an assembly for worship.