30秒学会 JavaScript 片段 · 2022年5月8日

30秒学会 JavaScript 片段 – Create directory if not exists

Creates a directory, if it does not exist.

  • Use fs.existsSync() to check if the directory exists, fs.mkdirSync() to create it.

代码实现

import { existsSync, mkdirSync } from 'fs';

const createDirIfNotExists = dir =>
  !existsSync(dir) ? mkdirSync(dir) : undefined;

createDirIfNotExists('test');
// creates the directory 'test', if it doesn't exist

翻译自:https://www.30secondsofcode.org/js/s/create-directory-if-not-exists