The same magic that makes James' wishes come true - traveling to New York, having a large group of friends that also act as a caring, surrogate family - also brings to life his fears. The mechanical shark spits out the same plates of disgusting fish heads Spiker and Sponge fed him, and threatens to prevent his escape to a better life just as they do in the film's conclusion.
Also, when James first meets the bugs in the film, only Miss Spider knows his real name (the others didn't know, or thought that his aunts' nicknames for him was his name.) It might be overlooked - however, when you find out that James befriended Miss Spider and saved her from his aunts in the beginning it makes sense that she knows his name. She would have heard his I Am Song, after all.