Sergiu
Sergiu

Reputation: 471

Node fs Error: EPERM: operation not permitted, open

I get this error in my app:

Error: EPERM: operation not permitted, open 'C:\Program Files (x86)\Full Menu\db\main.json'

The app I have is built with electron-boilerplate. I am using this function to get the path to the root of the app:

path.dirname(process.execPath)

And this is the script that writes the file:

fs.writeFile(apath + '/db/' + elem + '.json', JSON.stringify(results)

I know what the problem is: permissions. But how could I get this working without running the app as an administrator?

Upvotes: 46

Views: 132932

Answers (15)

Ryan Didevar
Ryan Didevar

Reputation: 678

I've faced recently such a problem in my code. After a harsh searching for solution, finally I've just build the project and after that, everything worked properly. I've just run the following code.

yarn build

or

npm run build

I don't exactly know, if it works for everyone facing such a problem, but hope it will.

Upvotes: -1

Irving Juárez
Irving Juárez

Reputation: 21

I was facing the same problem using the following software:

  • Windows 10
  • GitBash
  • Node v19

I was able to solve it opening GitBash as admin

Upvotes: 0

Suman Barua
Suman Barua

Reputation: 49

If you are facing this issue on Windows 10, then please try the following:

  1. Uncheck readonly options for the folder (if read-only reverts, login as administrator)
  2. Open terminal as administrator (if you are facing this issue on terminal)
  3. Switch off ransomware folder protection
  4. Change chmod of the folder
  5. Check if the folder is hidden or not
  6. Disable antivirus protection (temporarily) and try this
  7. Or move your project folder somewhere else, where antivirus ransomware protection is disable.

If nothing above works, then try the following: https://appuals.com/how-to-fix-folder-keeps-reverting-to-read-only-on-windows-10/.

Hope this would of help.

Upvotes: 4

Robin
Robin

Reputation: 11

I had the same problem, when i tried to create and write to a file using NodeJS. I thought it had to do with my windows file/folder access permissions, but after restarting my computer and running the code again, I still got the same error.

enter image description here

enter image description here

However, this time around my antivirus gave me a pop-up message also, stating that it blocked permission for Node.exe to write or open files. So once I flagged Node.exe as safe for my anti-virus program (Avast).

It worked for me. Disabling my antivirus could've also temporarily fixed it, I guess.

Upvotes: 0

allen
allen

Reputation: 456

On my Windows 10 machine, I encountered this error when running an old Node JS project. I think Node version 10.16.

In any case, it was trying to modify a dotfile in my project. Be sure that the file isn't hidden on Windows. After unchecking the hidden option in the file properties pop up. Everything worked.

So to fix:

  1. Right click file in Windows Explorer
  2. Select properties
  3. Uncheck Hidden
  4. Click Ok
  5. Re-run your command.

enter image description here

Upvotes: 21

this is not an exact answer but may help:

i think if you want to read or readSync a file that doesn't exist you will encounter an EPERM error...

in many programming languages, any permission related error may not directly means an actual permission issue

for example in PHP Folders (not files) must delete by php rmdir() method but if you want to do that with unlink() , u will encountered with a wrong Warning message that says "permission denied"

Upvotes: 1

Shivam Gupta
Shivam Gupta

Reputation: 593

I face this issue when I was deleting a file/folder.

Solution:

Just restart your code editor/ terminal Or Restart your computer

Upvotes: 6

Stem Florin
Stem Florin

Reputation: 872

If you have the file that you can't open or modify mounted as a volume in docker restarting docker should fix the issue.

Upvotes: 16

mmcfly
mmcfly

Reputation: 878

I had this issue too. I'm using TFS (or VSO, Azure DevOps, etc.) for source control. I was trying to compile from .scss to .css and it couldn't open my .css. I just needed to right-click on my .css file and Check Out for Edit...

Upvotes: 1

George
George

Reputation: 301

Restarting my computer fixed this problem for me.

Upvotes: 1

clood
clood

Reputation: 27

I had the error because i have already open the file before

var stream = fs.createWriteStream(outputFileName, {flags:'a'})
var output = fs.createWriteStream(outputFileName, {flags:'a'})

Upvotes: 0

Squidward Tentacles
Squidward Tentacles

Reputation: 900

If you use windows 10, you must turn off Ransomware protection. Ransomware protection will prevent all folder and file changes.You can turn off it in Windows Security Center. See screenshot below:

enter image description here

Upvotes: -3

Rainhider
Rainhider

Reputation: 836

i had to run the node command prompt as administrator and that fixed the issue.

Upvotes: 10

JJJCoder
JJJCoder

Reputation: 16956

For the benefit of searchers; I has this error. I added full permissions for Everyone as a test, but that didn't fix it. The issue was that the file was set to readonly (by source control).

Unchecking the readonly option in the file properties fixed the issue.

Upvotes: 54

Metux
Metux

Reputation: 200

I think that you must change the permissions recursively to the file so the user executing your script can read / write this file.

https://fr.wikipedia.org/wiki/Chmod

Upvotes: 0

Related Questions