Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda