How to fix ESP-IDF fatal error: nvs_flash.h: No such file or directory

Problem

When building an ESP-IDF project, you may encounter the following error:

main.cpp
/home/user/MyProject/main/main.cpp:6:10: fatal error: nvs_flash.h: No such file or directory
    6 | #include <nvs_flash.h>
      |          ^~~~~~~~~~~~~
compilation terminated.
```plaintext {filename="main.cpp"}

## Solution

You need to ensure that the `nvs_flash` component is included in your project. This can be done by modifying your `main/CMakeLists.txt` file to include the `nvs_flash` component as a dependency.

You can do this by adding the `REQUIRES nvs_flash` line in your `idf_component_register` function. Here’s how to modify your `main/CMakeLists.txt` file:

```cmake {filename="idf_component_register_nvs_flash.cmake"}
idf_component_register(
  SRCS "main.cpp"
  INCLUDE_DIRS "../include"
  REQUIRES nvs_flash
)
```plaintext {filename="idf_component_register_nvs_flash.cmake"}

Check out similar posts by category: ESP-IDF, ESP32