Kinda Code
Home/Node/Node.js: Ways to Create a Directory If It Doesn’t Exist

Node.js: Ways to Create a Directory If It Doesn’t Exist

Last updated: July 12, 2022

This article walks you through a couple of different ways to create a directory if it doesn’t exist in Node.js.

Using the FS module

FS, which stands for “file system”, is a standard module of Node.js so you can use it without installing any third-party packages. The module provides some useful methods:

  • existsSync: Used to synchronously check whether a file or a directory exists.
  • mkdirSync: Used to synchronously create a new directory with a given name.
  • exists: Used to asynchronously test if a given path exists. This one is deprecated, you can still use it with no errors but you should no longer call it in new projects.
  • mkdir: Used to asynchronously create a new directory.

Example 1

// kindacode.com

import fs from 'fs'
// is you're using CommonJS, use the line below:
// const fs = require('fs');

const directory = './some-dir'
if(!fs.existsSync(directory)){
  fs.mkdirSync(directory)
}

And here’s the project structure after that:

.
├── Readme.md
├── package-lock.json
├── package.json
├── some-dir
└── src
    └── index.js

Example 2

// kindacode.com

import fs from 'fs'
// is you're using CommonJS, use the line below:
// const fs = require('fs');

const dir = './new-folder'
fs.mkdir(dir, (err) => {
  if(err){
    console.log('The directory already exists!')
  } else {
    console.log('Successfully created a new directory')
  }
})

In the examples above, the path of the directory is the related path to your package.json file.

Using a third-party package

What if you want to create a directory that nests deep in many other directories that even don’t exist? For instance, the path is:

const dir = './some/deep/path/kindacode/node-tutorials'

The solutions are you can perform multiple steps iteratively using the above method or using a third-party library that can help us get the job done with only 1 or 2 lines of code. There are so many names to choose from, such as make-dir, makdirp, and fs-extra.

In the example below, we’ll use fs-extra. You can add it to your project by running:

npm i fs-extra

Using the ensureDir method provided by the library to ensure that a given directory exists. If the directory structure does not exist, it will be created:

import fse from 'fs-extra'

// if you're a fan of CommonJS, use "require"
// const fse = require('fs-extra');

const dir = './some/deep/path/kindacode/node-tutorials'
fse.ensureDir(dir);

Conclusion

We’ve gone through a few ways of creating a new directory if it doesn’t exist with Node.js. If you are a backend developer then the occasions that you need them are not rare.

Node.js is awesome and many people love it. If you’d like to learn more new and interesting things about this Javascript runtime, take a look at the following articles:

You can also check out our Node.js category page or PHP category page for the latest tutorials and examples.