Open In App

How to solve “File writing permissions blocked by the EPERM” issue in Node.js ?

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In Node.js, while writing a file, the EPERM error may occasionally prevent the file’s writing rights. This error happens when a process does not have the appropriate permissions to carry out a certain action on a file. In this situation, writing to the file is prohibited and it cannot be rewritten.

Problem: Let’s assume we want to use Node.js to write to a file called “data.txt” which is located in our project directory. But, if the file writing rights are restricted and we attempt to write to this file, we can receive the EPERM error.

Using the fs.writeFile() function, we try to write the string “Hello World!” to the file “data.txt”. The error message is sent to the console if it happens during the write operation. A success message is produced following a successful write operation. The writing process is halted and the EPERM error occurs, though, if the file’s writing rights are restricted.

 

Javascript




const fs = require('fs');
  
// Trying to write to 'data.txt' file
fs.writeFile('data.txt', 'Hello World!', (err) => {
  
    // If an error occurs
    if (err) {
  
        // Print the error message
        console.log(err);
  
        // Otherwise
    } else
  
        // Print success message
        console.log('Data has been written to the file successfully.');
    }
});


Output:

 

Solution: We may first remove the current file and then create a new file with the same name to get around the EPERM problem. The file can be deleted using the fs.unlink() function, and a new file can then be written using the fs.writeFile() method.

Javascript




const fs = require('fs');
  
// First, try to delete the existing 'data.txt' file
fs.unlink('data.txt', (err) => {
  
    // If an error occurs, print the error message
    if (err && err.code !== 'ENOENT') {
        console.log(err);
  
    // Otherwise, print success message
    // and try to write a new file
    } else {
        console.log('File deleted successfully.');
  
        // Now try to write to the 'data.txt' file
        fs.writeFile('data.txt', 'Hello World!', (err) => {
  
            // If an error occurs, print the error message
            if (err) {
                console.log(err);
  
            // Otherwise, print success message
            } else
                console.log('Data has been written '
                    + 'to the file successfully.');
            }
        });
    }
});


Output

 

Output

Using the fs.unlink() technique, this revised code first tries to erase the already-existing “data.txt” file. The error message is sent to the console if it happens during the delete process. The error notice is ignored if the file doesn’t exist, which frequently happens when the code is executed for the first time.

If the delete operation is successful, the fs.writeFile() function is used to create a new file with the same name and prints a success message to the console. The error message is sent to the console if it happens during the write operation. The console prints a success message if the write operation is successful.

By removing the current file and generating a new one with the same name, this method guarantees that any file writing rights difficulties are avoided and the EPERM error is not generated.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads