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

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hello Brother,

Hello Brother, 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 lesson 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 lesson

Lesson 1: Create Node App

run bellow command and create node app.

mkdir my-app

cd my-app

npm init

Lesson 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

Result:-

it will create images directory on root folder.

thank you very much for following up with me.

Tags

Recent Posts

Related Posts