Skip to main content

createDirIfNotExistsSync

TS NODE

Creates a directory, if it does not exist.

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

typescript
const { mkdirSync, existsSync } = require("fs");

const createDirIfNotExistsSync = (dir: string) =>
!existsSync(dir) ? mkdirSync(dir) : undefined;
typescript
createDirIfNotExistsSync("test"); // creates the directory 'test', if it doesn't exist