【问题标题】:Rename return "EPERM: operation not permitted" in JavaScript在 JavaScript 中重命名返回“EPERM:不允许操作”
【发布时间】:2016-08-25 03:55:08
【问题描述】:

在我的 windows 服务器中,我发现 fs.rename 函数总是返回以下错误:

错误代码是 Error: EPERM: operation not allowed, rename 'C:\javascript\nodejs\a.txt' -> 'C:\javascript\nodejs\b.txt'

以下是测试代码:

var fs = require('fs');

fs.writeFileSync('a.txt',"This is a file")
fs.writeFileSync('b.txt',"This is another file")

fs.rename('a.txt','b.txt',function (err) {
            console.log("error code is " + err);
       });

var text = fs.readFileSync('b.txt', "utf-8");

console.log(text) 

但是,在当前文件夹中,我确实看到了原始文件“a.txt”以及新的重命名文件“b.txt”。

【问题讨论】:

  • 在同步读取目标位置的同时进行异步重命名可能不是一个好主意。
  • 谢谢Bergi,你是对的。我没有注意到这一点。实际上我正在使用上面的代码来测试服务器的权限。顺便说一句,我可以在 Stackoverflow 中为您的 cmets 投票吗?

标签: javascript node.js


【解决方案1】:

fs.rename 只是rename 的包装,根据rename docs

EPERM or EACCES
  The directory containing oldpath has the sticky bit (S_ISVTX)
  set and the process's effective user ID is neither the user ID
  of the file to be deleted nor that of the directory containing
  it, and the process is not privileged (Linux: does not have
  the CAP_FOWNER capability); or newpath is an existing file and
  the directory containing it has the sticky bit set and the
  process's effective user ID is neither the user ID of the file
  to be replaced nor that of the directory containing it, and
  the process is not privileged (Linux: does not have the
  CAP_FOWNER capability); or the filesystem containing pathname
  does not support renaming of the type requested.

您可能无权删除该文件或目标文件名已存在。

【讨论】:

  • 投反对票,因为 OP 显然是在谈论 Windows,而这个答案是指 linux,
【解决方案2】:

经过巨大的努力为我找到了解决方案..?

“创建新文件夹” mkdir E:\Buildagent\npm mkdir E:\Buildagent\npm-cache

"移动 npm 前缀和缓存" robocopy c:\Users\the-build-user\AppData\Roaming\npm E:\Buildagent\npm robocopy c:\Users\the-build-user\AppData\Roaming\npm-cache E:\Buildagent\npm-cache /E /MOVE

“更新前缀和缓存的 npm 配置” npm 配置设置前缀 E:\Buildagent\npm npm config 设置缓存 E:\Buildagent\npm-cache

https://alastaircrabtree.com/fixing-intermittant-eperm-operation-not-permitted-on-npm-install/

【讨论】:

    猜你喜欢
    • 2018-07-12
    • 1970-01-01
    • 2017-01-10
    • 2020-09-10
    • 2021-01-01
    • 1970-01-01
    • 2020-08-06
    • 1970-01-01
    • 2019-10-20
    相关资源
    最近更新 更多