I suggest you read this. Perry Bebbington did an excellent illustrated article on Flyback diodes and why you need them which includes diode placement.
Next when you find the suggested MOSFET you want about a 10K resistor gate to ground. Actually if this is a low current motor I would just consider a common NPN switching transistor like a 2N2222 rather than worry about the voltage gate source of a MOSFET. I would not power the motor using the ESP32 board.
Ron