Files
30-seconds-of-code/snippets/create-dir-if-not-exists.md
2023-04-28 22:29:23 +03:00

24 lines
495 B
Markdown

---
title: Create directory if not exists
type: snippet
tags: [node]
cover: sunrise-over-city
dateModified: 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.
```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
```