重命名返回" EPERM:不允许操作"在JavaScript中

时间:2016-08-25 03:55:09

标签: javascript node.js

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

  

错误代码是错误:EPERM:不允许操作,重命名' 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"。

2 个答案:

答案 0 :(得分:0)

经过巨大的斗争,我找到了解决方案..

"创建新文件夹" 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 config set前缀E:\ Buildagent \ npm npm config set cache E:\ Buildagent \ npm-cache

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

答案 1 :(得分:-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.

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