0

I'm trying to create a folder if it does not exist, using NodeJs. But I'm getting Error: ENOENT: no such file or directory, mkdir when trying to create directory error. How can I fix it?

const folderName = `./images/logger`;

try {
  if (!fs.existsSync(folderName)) {
    fs.mkdirSync(folderName);
  }
} catch (err) {
  console.error(err);
}

1 Answer 1

3

You need to add {recursive:true} option since you want to create more than one directory:

const folderName = `./images/logger`;

try {
if (!fs.existsSync(folderName)) {
  fs.mkdirSync(folderName,{recursive:true});
}
} catch (err) {
 console.error(err);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.