Node Js Create Directory / Folder if Not Exists Tutorial

While working on the Node project, you create various directories. Creating a new directory is not so tricky. However, sometimes, we can make this process effortless and subtle.

In this tutorial, you will learn how to comfortably create the directory if not exist in the Node js application using various methods.

We are going to show you three diffrent yet eloquent methods. We will use the fs module to create a directory, use the fs mkdir method to create a directory. Furthermore, we will show you the utter process of generating a new directory if not exist in node js.

FS is a reliable file system module that amplifies the experience of working with node. If you use the fs module, you do not have to install an external package. It offers some handy methods to handle the directory-related task in node.

In this profound guide, we will take the help of mkdirSync, existsSync, exists, and mkdir methods and profoundly check if the directory exists and, if not, then create the directory in node.

Different Methods to Create Directory If Not Exists in Node Js

  • Step 1: Create Project Folder
  • Step 2: Set Up Package.json
  • Step 3: Create App Js File
  • Step 4: Create Directory with Fs Extra
  • Step 4: Create Directory with mkdir
  • Step 4: Create Directory with FS Module
  • Step 6: Test Node Script

Create Project Folder

In the first step, you need to open the terminal, on your commmand prompt type the command execute the command.

mkdir node-demo

We have successfully created the node project folder, now enter into the direcotry.

cd node-demo

Set Up Package.json

After you entered into the project folder, we have to next run the npm init command, this command will generate the new package.json file.

npm init

The above command created the package.json file, this file is the locus of scripts, and the important project meta data.

Create App Js File

In this step, we will create the app.js file, in this script file we will define the logic to generate new directory. Or you can also, add other feature related code.

To make the directory, we will execute the command from the command line tool. Therefore, we require to register the app.js file in scripts tag.

Head over to package.json file and add the given code into the file.

  "scripts": {
    "start": "node app.js"
  },

Create Directory with Fs Extra Package

In this step, we will show a completely diffrent approach. We will install the fs-extra library.

npm install fs-extra

The fs-extra package offers the flexibility that you do not get with the fs (file system) module. This library comes with promise support to the fs methods.

We will show you how to create the directory in nested folders, we are using the ensureFile() method.

You have to add the following code in the app.js file.

let fsx = require('fs-extra')

let createDir = './folder/img/folder1//node-dir';

fsx.ensureDir(createDir);

Ensures that the file exists. If the file that is requested to be created is in directories that do not exist, these directories are created. If the file already exists, it is NOT MODIFIED.

Create Directory with FS Module

Next, we will use the fs.existsSync() method and synchronously find out if a file already exists on a provided path; if a file exists, it will return boolean output.

Based on the boolean result, we will create the uploads folder through command line tool.

const fs = require('fs')

const DIR = './uploads'

if(!fs.existsSync(DIR)){
  fs.mkdirSync(DIR)
}

You can see the folder will be created in your node app something like this:

...
...
├── package-lock.json
├── uploads
├── package.json
└── app.js
...
...

Create Directory with mkdir

In this last code example, we will create the directory in node asynchronously using the fs.mkdir() method, we will name the folder images.

let fs = require('fs')

let DIR = './images'

fs.mkdir(DIR, (error) => {
  if(error){
    console.log('Folder exists already.')
  } else {
    console.log('New folder has been created.')
  }
})

After you run the node command, an images folder will be generated at the root of your node project.

...
...
├── images
├── package.json
├── package-lock.json
└── app.js
...
...

Test Node Script

We have implemented all the code, showed you various methods to formulate the directory in node. Now, eventually, we can test out the app hence execute the command.

node app.js

Summary

Node Js Create Directory / Folder if Not Exists Tutorial

This comprehensive guide has shared some of the easy and best solutions to make a new directory in node. We used the built-in node fs module and mkdir to generate the directory.

Also, we have installed and used the external npm package to check if the directory exists; if not, create the new directory folder in the node.