ENOENT, no such file or directory on fs.mkdirSync
When any folder along the given path is missing, mkdir
will throw an ENOENT
.
There are 2 possible solutions (without using 3rd party packages):
- Recursively call
fs.mkdir
for every non-existent directory along the path. - Use the
recursive
option, introduced in v10.12:fs.mkdir('./path/to/dir', {recursive: true}, err => {})
Solve here How to create full path with node's fs.mkdirSync?
NodeJS version 10.12.0 has added a native support for both mkdir and mkdirSync to create a directory recursively with recursive: true option as the following:
fs.mkdirSync(targetDir, { recursive: true });
And if you prefer fs Promises API, you can write
fs.promises.mkdir(targetDir, { recursive: true });
When you are using fs.mkdir
or fs.mkdirSync
, while passing the path like folder1/folder2/folder3
, folder1
and folder2
must exist otherwise you will get the above error.