How to initialize LittleFS in PlatformIO on the ESP32 using the lorol/LittleFS library

***Note: I don’t recommend using the loros/LittleFS library when using an up-to-date version of the arduino-esp32 framework such as 2.0.5 -***the newer versions of this framework come with an embedded LittleFS framework. See TODO for instructions how to initialize the library.

Currently you need to add the LittleFS-ESP32 library in platformio.ini (the library is available as part of the core arduino-espressif32 bleeding edge version but you need that library in the standard version):

lib_deps =
    lorol/LittleFS_esp32 @ ^1.0.6

Now include LittleFS:

#include <LITTLEFS.h>

#define SPIFFS LITTLEFS

Initialize it using

// Initialize LittleFS
if (!LITTLEFS.begin(false /* false: Do not format if mount failed */)) {
  Serial.println("Failed to mount LittleFS");
  if (!LITTLEFS.begin(true /* true: format */)) {
    Serial.println("Failed to format LittleFS");
  } else {
    Serial.println("LittleFS formatted successfully");
  }
} else { // Initial mount success
}