Create directory if it doesn't exist

Published on October 28th, 2019

javascript icon

When you are creating directories and files, most likely on startup of your node program, it is useful to check if they already exists. It’s also very easy to implement. All you need is one function.

const fs = require('fs')

const createDirectoryIfItDoesntExist = (directoryName) => {
  return !fs.existsSync(directoryName)
    ? fs.mkdirSync(directoryName)
    : undefined

fs.existsSync will check if directory is present and fs.mkdirSync will create directory if it needs to.