ItSolutionStuff.com

How to Create Directory if does not Exists in Node JS?

By Hardik Savani • September 16, 2021
Node JS

Hello,

Today, node js create directory if not exists is our main topic. you'll learn how to create directory in node js. This article goes in detailed on how to create a new directory in node js. this example will help you node js create folder if not exists.

We will use fs npm package for folder create using node.js. fs package provide mkdirSync() for creating folder. let's see simple example

Step 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Step 2: Create server.js file

server.js

const fs = require('fs');

var folder = './images';

if (!fs.existsSync(folder)){

fs.mkdirSync(folder);

console.log('Folder Created Successfully.');

}

now you can simply run by following command:

node server.js

Output:

it will create images directory on root folder.

i hope it can help you...

Tags: Node JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get All Files from Folder in Node JS?

Read Now →

How to Download Image from URL in Node JS?

Read Now →

How to Generate QR Code in Node JS?

Read Now →

Node JS Send Email with Attachment Example

Read Now →

How to Get Data from Json File in Node JS?

Read Now →

How to Remove Element from Array in Node JS?

Read Now →

How to Push Element in Array in Node JS?

Read Now →

How to use Underscore JS in Node JS App?

Read Now →

Node JS Sort Array of Objects by Value Example

Read Now →

How to Create and Use .env File in Node JS?

Read Now →

Node JS Resize Image Before Upload using Multer Sharp

Read Now →