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

360
Views
Javascript Error: ENOENT: no such file or directory

I'm trying to create a directory, then create a file in this directory, then I'm trying to delete the directory. If I'm running the functions one by one, the code is working, but when I'm running them together I get this error :

[Error: ENOENT: no such file or directory, open 'C:\Users\Andrei\Desktop\tw\test\test.txt'] { errno: -4058, code: 'ENOENT', syscall: 'open', path: 'C:\Users\Andrei\Desktop\tw\test\test.txt' }

Also if I'm running only the first 2 functions, everything works fine. What's wrong with my code?

const rimraf = require("rimraf");
const mkdirp = require("mkdirp");
const fs = require("fs");
const err1 = new Error("File Error!");
const err2 = new Error("Directory deleting Error!");

function createDirectory() {
    mkdirp("./test");
}
function createFile() {
    fs.writeFile("./test/test.txt", "this is my text", function (err1) {
        if (err1) throw err1;
        console.log("Results Received");
    });
}
function deleteDirectory() {
    rimraf("./test", function (err2) {
        if (err2) console.log(err2);
        console.log("Succesfully deleted a directory");
    });
}

function main() {
    createDirectory();
    createFile();
    deleteDirectory();
}
main();
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Those functions are async. You'll need to use some Promise logic to make them chain:

const rimraf = require("rimraf");
const mkdirp = require("mkdirp");
const fs = require("fs");
const err1 = new Error("File Error!");
const err2 = new Error("Directory deleting Error!");

function createDirectory() {
    return mkdirp("./test");
}
function createFile() {
    return new Promise((resolve) => {
      fs.writeFile("./test/test.txt", "this is my text", function (err1) {
        if (err1) throw err1;
        console.log("Results Received");

        resolve();
      });
   });
}
function deleteDirectory() {

    return new Promise((resolve) => {
      rimraf("./test", function (err2) {
        if (err2) console.log(err2);
        console.log("Succesfully deleted a directory");
       
        resolve();
      });
   });
}

function main() {
    createDirectory()
    .then(() => {
        return createFile();
    })
    .then() => {
       return deleteDirectory();
    });
}
main();
about 4 years ago · Juan Pablo Isaza Report

0

mkdirp, writeFile, and rimraf ar all asynchronous, but you're not waiting for earlier ones to finish before you ask the later ones to start.

I'd suggest using an async functoin and the promise-enabled versions of those (mkdirp is already promise-enabled, use writeFile from fs/promises, and there has to be a promise wrapper for rimraf now; if not, use util.promisify). Then you could use await:

async function main() {
    await createDirectory();
    await createFile();
    await deleteDirectory();
}

where (for example) createFile is:

const {writeFile} = require('fs/promises');
// ...
async function createFile() {
    await writeFile("./test/test.txt", "this is my text");
    console.log("Results Received");
}
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!