0

I have a node.js application where I have a function that will create folders if they don't exist and then create files if it doesn't exist. I also have a function that will delete those files and folders. My problem is that when the folders are created they are created as read-only.

I have already tried to use different file permission modes in the option when creating the folder and made sure to create the files with read/write permission.

This is the code that will create the folders and files:

mkdirp.sync(path.join(__dirname, filePath));
for (let i = 0; i < files.length; i++) {
    let filePaths = [] // this list will include the filepath for each file             
    await fs.open(path.join(__dirname, filePaths[i]), "w+", function(error, fd) {
        if (error) {
            console.error("error writing image: \n\n" + error);
            return;
        }
        fs.writeSync(fd, files[i].buffer, null, "base64");
    });
}

This is the code that will delete folders and files:

await del(filePath);

Expected result should be that the application can create and delete files and folders. The actual result is that the application gets an error message saying this:

{ [Error: EPERM: operation not permitted, unlink "filepath"]
  errno: -4048,
  code: 'EPERM',
  syscall: 'unlink',
  path: "filepath"
}
error writing image:

Error: EPERM: operation not permitted, open "filePath"

The application has also been tried on Ubuntu where it runs fine.

Gaute Haugen
  • 103
  • 1
  • 2
  • 15
  • 1
    possible dup of https://stackoverflow.com/questions/21176733/what-the-scenario-call-fs-close-is-necessary – AZ_ Apr 10 '19 at 09:00
  • This gave me the answer I was looking for. I thought the fs.close() was to set what happened when fs closed and not to actually close fs. – Gaute Haugen Apr 10 '19 at 09:07

0 Answers0