Too little information. Which WiFi module (there are several)? How do you communicate with the WiFi module (SPI, SoftwareSerial, or Serial)?
If (as I suspect from the error) it is Serial you need to disconnect the WiFi module while you are uploading a new sketch to your Mega. Otherwise the WiFi module is also responding to the communication between avrdude and the 2560 confusing everyone in the process.