Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
directory is not creating

Am trying to create a directory with subfolders in my application. The new request will create folders only if the parent folder is already there but not creating if root folder is not there.

import { mkdir } from 'fs';

  mkdir(join(__dirname, '../folder_to_create_directory/', req.body.path), (err) => {
        if (err) {
            return "error";
        }
        return "success"
    });

The req.body.path is a path string eg: test/folder/subfolder. The code will work only if we create the "test" folder manually (it is not returning "success" message too even though the directory is being created). IF the test folder is not there then the directory is not creating.

expected output:-

folder_to_create_directory/test/folder/subfolder

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use fs library to work with a file system.

For nested dirs:

var fs = require('fs');
var dir = join(__dirname, '../folder_to_create_directory/', req.body.path);

if (!fs.existsSync(dir)){
    fs.mkdirSync(dir, { recursive: true });
}

Or, for individual dirs:

var fs = require('fs');
var dir = join(__dirname, '../folder_to_create_directory/', req.body.path);

if (!fs.existsSync(dir)){
    fs.mkdirSync(dir);
}
about 4 years ago · Juan Pablo Isaza Report

0

you are missing an option "{recursive: true}". Try this example:

const { mkdir } = require("fs");
const {join} = require('path')

const path = join(__dirname, "../folder_to_create_directory", 'test/folder/subfolder')

mkdir(path, { recursive: true }, (err) => {
  if (err) {
    return "error";
  }
  return "success";
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!