node.js 节点 fs 错误:EPERM:不允许操作,打开

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33419474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:27:03  来源:igfitidea点击:

Node fs Error: EPERM: operation not permitted, open

node.jsfs

提问by Sergiu

I get this error in my app:

我在我的应用程序中收到此错误:

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

错误:EPERM:不允许操作,打开“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?

我知道问题是什么:权限。但是如何在不以管理员身份运行应用程序的情况下使其正常工作?

回答by HockeyJ

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

为了搜索者的利益;我有这个错误。我添加了完整权限Everyone作为测试,但这并没有解决它。问题是该文件被设置为readonly(通过源代码管理)。

Unchecking the readonlyoption in the file properties fixed the issue.

取消选中readonly文件属性中的选项修复了该问题。

回答by Metux

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

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

回答by Stem Florin

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

如果您在 docker 中无法打开或修改安装为卷的文件,则重新启动 docker 应该可以解决该问题。

回答by Rainhider

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

我不得不以管理员身份运行节点命令提示符并解决了这个问题。

回答by Squidward Tentacles

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:

如果您使用 Windows 10,则必须关闭勒索软件保护。勒索软件保护将阻止所有文件夹和文件更改。您可以在 Windows 安全中心将其关闭。请看下面的截图:

enter image description here

在此处输入图片说明

回答by clood

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'})

回答by George

Restarting my computer fixed this problem for me.

重新启动我的计算机为我解决了这个问题。

回答by mmcfly

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

我也有这个问题。我正在使用 TFS(或 VSO、Azure DevOps 等)进行源代码控制。我试图从 .scss 编译为 .css,但它无法打开我的 .css。我只需要右键单击我的 .css 文件并签出进行编辑...

回答by Shivam Gupta

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

我在删除文件/文件夹时遇到了这个问题。

Solution:

解决方案:

Just restart your code editor/ terminal Or Restart your computer

只需重新启动您的代码编辑器/终端或重新启动您的计算机

回答by ashkan nasirzadeh

this is not an exact answer but may help:

这不是一个确切的答案,但可能会有所帮助:

i thinkif you want to reador readSynca file that doesn't exist you will encounter an EPERMerror...

我想如果你想要read或者readSync一个不存在的文件你会遇到一个EPERM错误......

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

在许多编程语言中,任何与权限相关的错误可能并不直接意味着实际的权限问题

for example in PHPFolders (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"

例如在PHP文件夹(不是文件)中必须通过 phprmdir()方法删除,但如果你想用unlink(),你会遇到一个错误的警告消息,说“权限被拒绝”