Files
30-seconds-of-code/snippets/createDirIfNotExists.md
2022-02-13 18:10:27 +02:00

499 B

title, tags, firstSeen, lastUpdated
title tags firstSeen lastUpdated
Create directory if not exists node,beginner 2018-12-12T19:25:16+02:00 2020-10-22T20:23:47+03:00

Creates a directory, if it does not exist.

  • Use fs.existsSync() to check if the directory exists, fs.mkdirSync() to create it.
const fs = require('fs');

const createDirIfNotExists = dir => (!fs.existsSync(dir) ? fs.mkdirSync(dir) : undefined);
createDirIfNotExists('test');
// creates the directory 'test', if it doesn't exist