There must be something wrong with the installation of the IDE (I guess you use windows app) and the core for the ESP.
Board nodemcuv2 (platform esp8266, package esp8266) is unknown
I’d suggest using the “Windows ZIP file for non admin install” version of the IDE. Never had such kind of problems with that variant.
Your source code compiles with no errors in my set-up.