nodejs create folder if not exist code example

Example 1: node js create folder

var fs = require('fs');

const dir = './database/temp';
if (!fs.existsSync(dir)) {
	fs.mkdirSync(dir, {
		recursive: true
	});
}

Example 2: nodejs make directory

var fs = require('fs');
var dir = './tmp';
if (!fs.existsSync(dir)){
    fs.mkdirSync(dir);
}

// or if complains about existence and nesting doesn't matter:

var shell = require('shelljs');
shell.mkdir('-p', 'root/parent/tmp');

Example 3: how to create a folder using fs in node js

//Include fs module
const fs = require('fs');

const dir = './dir'
fs.mkdir(dir);