43

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?

Sergiu
  • 441
  • 1
  • 4
  • 7

14 Answers14

52

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.

JsAndDotNet
  • 16,260
  • 18
  • 100
  • 123
  • 1
    This is the current fix that I am having to do at the moment. TFS + VS2015 marks the file as `readonly` when you check anything in and this breaks my gulp build script. Marking the files as not being readonly solves the issue, though it does make VS complain a lot about the files saying that there is an `Existing or non-version controlled version of the file`. Be careful with this as you may lose changes or even files when getting latest version. – SaoBiz Mar 23 '17 at 00:14
  • 2
    Also had this issue, required to use Perforce (pray for me) and it regularly sets to read only when syncing files. Right Click > Untick Read Only fixed the issue. – John Mellor Jul 08 '19 at 03:07
20

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

allen
  • 446
  • 5
  • 9
  • 1
    Why does this have any effect on whether you can read from or write to a file? – Clonkex Jun 23 '22 at 01:57
  • @Clonkex -- Because your app needs the hidden file, but when it's set as "Hidden", it means it's not allowed to be opened. So if you uncheck the "Hidden" property, your app can use it now, although your app doesn't really OPEN it, but only USES it. – William Hou Apr 12 '23 at 17:02
  • @WilliamHou Well yeah, but I guess I'm asking why the "hidden" attribute has any effect on reading and writing. It's just a marker for directory listings. It should have zero impact on reading and writing. That's just nonsensical. The attribute isn't called "read-only" or "protected". – Clonkex Apr 12 '23 at 22:47
  • @Clonkex Oh, I see what you mean. The hidden file is actually what VSCode does behind the scenes, which is why it's HIDDEN. VSCode DEFINITELY OPENS and USES the hidden file for its basic settings, but it OPENS it behind the scene. That's why we cannot see it. But it does impact your app. – William Hou Apr 17 '23 at 19:00
15

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

Stem Florin
  • 896
  • 1
  • 7
  • 18
10

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

Rainhider
  • 806
  • 1
  • 17
  • 31
4

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

Solution:

Just restart your code editor/ terminal Or Restart your computer

Shivam Gupta
  • 533
  • 4
  • 9
4

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.

1

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

Metux
  • 202
  • 2
  • 9
  • I solved the problem by changing the NSIS script so it will allow me to choose another installation folder. – Sergiu Oct 29 '15 at 17:01
1

Restarting my computer fixed this problem for me.

George
  • 131
  • 1
  • 5
1

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...

mmcfly
  • 834
  • 8
  • 12
0

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'})
clood
  • 27
  • 1
  • 8
0

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"

0

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

-1

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.

matheusr
  • 567
  • 9
  • 29
Robin
  • 1
-3

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

  • Even if this did fix this issue it'd be risky. Don't make it easier for attackers to ransomware your system. Find another way. – jpvantuyl Jul 28 '20 at 15:12