Error: ENOENT: no such file or directory, mkdir when trying to create directory

Error: ENOENT: no such file or directory, mkdir when trying to create directory

我正在尝试使用 NodeJs 创建一个文件夹(如果它不存在)。但我收到 Error: ENOENT: no such file or directory, mkdir when trying to create directory 错误。我该如何解决?

const folderName = `./images/logger`;

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

您需要添加 {recursive:true} 选项,因为您要创建多个目录:

const folderName = `./images/logger`;

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