Telmo Trooper
Telmo Trooper

Reputation: 5694

Error running Node.js' fs.rename() inside of a loop

I'm developing an application that has to organize some uploaded files, I'm separating them in folders with the ID of their request (I'm using express-request-id to get this ID).

The problem is that everytime I have more than a single file the "moving" process fails and I can't seem to fix it.

let request_folder = path.resolve(tmp_folder + "/" + req.id);

/* Checking if the folder exists */
fs.access(request_folder, fs.constants.F_OK, function(error) {
    if(error) { // it doesn't
        /* Trying to create it */
        fs.mkdir(request_folder, function(error) {
            if(error) {
                console.log("Error: Couldn't create the directory.");
                console.log(error);
            }
        });
    }
});

/* Moving uploaded files to their respective request folder */
req.files.forEach(function(file) {
    let new_file_path = path.resolve(request_folder + "/" + file.filename);
    fs.rename(file.path, new_file_path, function(error) {
        if(error) {
            console.log("Error: Couldn't move " + file.filename + ".");
            console.log(error);
        }
    });
});

I'm one hundred percent sure both the folders and the files exist, but I when trying to move two files at once I get:

Error: Couldn't move Desert.jpg.
{ Error: ENOENT: no such file or directory, rename 'C:\Users\telmo.silva\csc-links\public\tmp\Desert.jpg' -> 'C:\Users\telmo.silva\csc-links\public\tmp\d2abf375-d09f-440c-a5ba-adf4f5725a73
\Desert.jpg'
  errno: -4058,
  code: 'ENOENT',
  syscall: 'rename',
  path: 'C:\\Users\\telmo.silva\\csc-links\\public\\tmp\\Desert.jpg',
  dest: 'C:\\Users\\telmo.silva\\csc-links\\public\\tmp\\d2abf375-d09f-440c-a5ba-adf4f5725a73\\Desert.jpg' }
Error: Couldn't move Chrysanthemum.jpg.
{ Error: ENOENT: no such file or directory, rename 'C:\Users\telmo.silva\csc-links\public\tmp\Chrysanthemum.jpg' -> 'C:\Users\telmo.silva\csc-links\public\tmp\d2abf375-d09f-440c-a5ba-adf4f
5725a73\Chrysanthemum.jpg'
  errno: -4058,
  code: 'ENOENT',
  syscall: 'rename',
  path: 'C:\\Users\\telmo.silva\\csc-links\\public\\tmp\\Chrysanthemum.jpg',
  dest: 'C:\\Users\\telmo.silva\\csc-links\\public\\tmp\\d2abf375-d09f-440c-a5ba-adf4f5725a73\\Chrysanthemum.jpg' }

Does anyone know what am I doing wrong? Thanks!

Upvotes: 2

Views: 986

Answers (2)

nem035
nem035

Reputation: 35501

The fs operations you are using are async meaning they can happen in any order.

Your loop will ask node to create a folder and move the files basically at the same time. In other words, it will run a synchronous loop that will dispatch all fs actions concurrently. Meaning that you have no guarantees what will actually run first.

Try creating the folder before moving all the files:

fs.access(request_folder, fs.constants.F_OK, function(error) {
    if(error) {
        return fs.mkdir(request_folder, function(error) {
            if(error) {
                return;
            }
            moveFiles();
        });
    }
    moveFiles();
});

function moveFiles() {
  req.files.forEach(function(file) {
    // ...
  });
}

Using promises might make this a bit cleaner:

const access = util.promisify(fs.access);
const mkdir = util.promisify(fs.mkdir);
const rename = util.promisify(fs.rename);

access(request_folder, fs.constants.F_OK)
  .then(moveFiles, makeDirAndMoveFiles)
  .catch(console.error);

function moveFiles() {
  return Promise.all(
    req.files.map(file => {
      const new_file_path = path.resolve(request_folder + "/" + file.filename);
      return rename(file.path, new_file_path);
    })
  )
}

function makeDirAndMoveFiles() {
  return mkdir(request_folder).then(moveFiles);
}

Upvotes: 3

Ankur
Ankur

Reputation: 205

It is possible you are trying to move the file even before the destination folder exists.

try the new await keyword to structure the code.

const {promisify} = require('util');

const fs = require('fs');
const accessFileAsync = promisify(fs.access);
const mkdirFileAsync = promisify(fs.mkdir);
const renameFileAsync = promisify(fs.rename);
try{
  await accessFileAsync(request_folder, fs.constants.F_OK);
}
catch(ex)
{
  await mkdirFileAsync(request_folder);
}
req.files.forEach(function(file) {
    let new_file_path = path.resolve(request_folder + "/" + file.filename);
    try{
      await renameFileAsync(file.path, new_file_path);
    }
    catch(ex)
    {
      console.log(ex);
    }
});

Upvotes: 1

Related Questions