Files
30-seconds-of-code/snippets/createDirIfNotExists.md
2020-09-15 21:52:00 +03:00

18 lines
412 B
Markdown

---
title: createDirIfNotExists
tags: node,beginner
---
Creates a directory, if it does not exist.
- Use `fs.existsSync()` to check if the directory exists, `fs.mkdirSync()` to create it.
```js
const fs = require('fs');
const createDirIfNotExists = dir => (!fs.existsSync(dir) ? fs.mkdirSync(dir) : undefined);
```
```js
createDirIfNotExists('test'); // creates the directory 'test', if it doesn't exist
```