2016-08-25 59 views
1

“EPERM操作不允许”在我的Windows服务器,我发现fs.rename函数总是返回下面的错误:重命名回报:在JavaScript

error code is Error: EPERM: operation not permitted, 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”。

+0

做一个异步重命名,而在同一时间同步从目标位置读取可能是不一个好主意。 – Bergi

+0

谢谢Bergi,你是对的。我没有注意到这一点。其实我正在使用上面的代码来测试服务器的权限。顺便说一句,我可以在Stackoverflow中投票您的意见吗? – Liang

回答

0

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. 

你可能没有权限删除的文件或已经存在的目标文件名。

0

后巨大的斗争找到了我的解决方案..

“创建新的文件夹” 的mkdir E:\ Buildagent \ NPM 的mkdir E:\ Buildagent \ NPM-缓存

“移动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配置设置缓存E:\ Buildagent \ NPM-缓存

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