Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

134
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda