node.js 错误!错误:EPERM:不允许操作,重命名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39293636/
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
npm ERR! Error: EPERM: operation not permitted, rename
提问by Robin
When I execute npm installI get this error
当我执行npm install我收到这个错误
npm ERR! Error: EPERM: operation not permitted, rename C:\projects******\node_modules\react-async-script' -> 'C:\projects*******\node_modules.react-async-script.DELETE'
错误!错误:EPERM:不允许操作,重命名 C:\projects******\node_modules\react-async-script' -> 'C:\projects*******\node_modules.react-async-script 。删除'
- I am running the cmd as administrator
- I ran
npm cache cleanbefore - I made sure all the other applications don't have anything related to
node_modulesopen
- 我以管理员身份运行cmd
- 我
npm cache clean之前跑过 - 我确保所有其他应用程序都没有与
node_modules打开 相关的任何内容
回答by Roman
In my situation this helped. Before proceeding to execute these commands close all VS Code instances.
在我的情况下,这有帮助。在继续执行这些命令之前,请关闭所有 VS Code 实例。
clean cache with
npm cache clean --forceinstall the latest version of npm globally as admin:
npm install -g npm@latest --forceclean cache with
npm cache clean --forceTry to install your component once again.
清理缓存
npm cache clean --force以管理员身份全局安装最新版本的 npm:
npm install -g npm@latest --force清理缓存
npm cache clean --force再次尝试安装您的组件。
I hope this fix your issue. If not, you may temporarily disable your antivirus program and try again.
我希望这能解决您的问题。如果没有,您可以暂时禁用防病毒程序并重试。
回答by sree
I got it working when tried npm install with a force option to fetch remote resources even if a local copy exists on disk. Try running
尝试使用强制选项 npm install 获取远程资源时,即使磁盘上存在本地副本,我也能正常工作。尝试跑步
npm install --force
UpdatedFor some of my colleagues this solution was not working. But we tried using yarninstead of npm. It works (faster as well) without any issues all the time
更新对于我的一些同事,此解决方案不起作用。但是我们尝试使用yarn而不是npm。它始终有效(也更快)而没有任何问题
yarn install [package-name]
回答by Amit Bhosle
Not package.json, but for whatever reason, my node_modules/had become read-only. Resetting that fixed this.
不是package.json,但无论出于何种原因,我的node_modules/已成为只读。重置解决了这个问题。
回答by Ahmed Elkoussy
If you want to avoid the --forceoption (which is always a better approach), I suggest making sure that you have stopped running the project, as this is usually the main reason for locking the files in almost 90% of the cases I have seen
如果你想避免这个--force选项(这总是更好的方法),我建议确保你已经停止运行项目,因为这通常是我见过的几乎 90% 的情况下锁定文件的主要原因
I suggest the following steps in this order:
我建议按此顺序执行以下步骤:
1- In Angular stopping ng sand in React stopping npm startusually solves this issue because usually this error happens if a development server is running the project as it locks some files & then npm can't update them thus throwing this error
1- 在 Angular 停止ng s和 React 中停止 npm start通常可以解决这个问题,因为如果开发服务器正在运行项目,因为它锁定了一些文件,然后 npm 无法更新它们,因此通常会发生此错误,从而引发此错误
2- If the above doesn't work, then try closing the code editor that has the workspace opened in it(maybe it was locking some files or something)
2-如果上述方法不起作用,请尝试关闭其中打开了工作区的代码编辑器(可能是锁定了某些文件或其他内容)
So try closing the code editor & running:
所以尝试关闭代码编辑器并运行:
npm install
3- If still it doesn't work, then maybe you can try the --forceoption
3-如果仍然不起作用,那么也许您可以尝试该--force选项
npm install --force
回答by vmaldosan
I was getting that same error and according to https://github.com/Medium/phantomjs/issues/19it could be caused by your antivirus software. I disabled mine for the duration of the install and executed "npm install" on cmd as admin and it worked. Hope this helps.
我遇到了同样的错误,根据https://github.com/Medium/phantomjs/issues/19,这可能是由您的防病毒软件引起的。我在安装期间禁用了我的,并以管理员身份在 cmd 上执行了“npm install”并且它工作正常。希望这可以帮助。
回答by Sifat Haque
For me i just closed the Code editor (VS Code) and then run the same command. And that solves the issue for me.
对我来说,我只是关闭了代码编辑器(VS Code),然后运行相同的命令。这为我解决了这个问题。
回答by christo8989
I was getting the same thing. I didn't find this anywhere but it hit me that our VMs tend to change files to a read-only state. So I opened package.json's file properties and deselected Read-onlyin the "General" tab.
我得到了同样的东西。我在任何地方都没有找到这个,但我发现我们的虚拟机倾向于将文件更改为只读状态。所以我打开了package.json的文件属性并Read-only在“常规”选项卡中取消选择。
Steps
脚步
- Go to the
package.jsonfile. - Right-click and select Properties.
- In the Generaltab, Attributessection, deselect Read-only.
- Click Applyto apply the change.
- 转到
package.json文件。 - 右键单击并选择属性。
- 在General选项卡的Attributes部分中,取消选择Read-only。
- 单击应用以应用更改。
回答by jaycer
As Martin Kearn pointed out in a comment, closing Visual Studioresolved these npm permission issues for me. Evidently VS sometimes locks files in node_modules.
正如 Martin Kearn 在评论中指出的那样,关闭 Visual Studio为我解决了这些 npm 权限问题。显然 VS 有时会将文件锁定在node_modules.
回答by Bruno Guaitanele
This is a typical error caused by Antivirus. There is a workaround for cases like mine, where I can't disable A/V (Company Policy).
这是由 Antivirus 引起的典型错误。对于像我这样无法禁用 A/V(公司政策)的情况,有一种解决方法。
You have to change the polyfills.jsinside Npmpackage:
您必须更改polyfills.js内部Npm包:
[NODE_HOME]/node_modules/npm/node_modules/graceful_fs/polyfills.js
[NODE_HOME]/node_modules/npm/node_modules/graceful_fs/polyfills.js
Look for this statement:
查找此语句:
if (process.platform === "win32") {
Inside of this statement, there is a timeout making a retry in case of error. The problem is that in some cases, after the timeout, the file is still locked by the A/V. The solution is rip out the timeout and let this statement in loop. The change with the previous code commented:
在这个语句中,有一个超时会在出现错误时重试。问题是在某些情况下,超时后文件仍被 A/V 锁定。解决方案是删除超时并让此语句循环。与先前代码的更改注释:
if (platform === "win32") {
fs.rename = (function (fs$rename) { return function (from, to, cb) {
var start = Date.now()
var backoff = 0;
fs$rename(from, to, function CB (er) {
if (er
&& (er.code === "EACCES" || er.code === "EPERM")
/*&& Date.now() - start < 60000*/) {
console.log("Retrying rename file: " + from + " <> " + to)
fs$rename(from, to, CB);
/*setTimeout(function() {
fs.stat(to, function (stater, st) {
if (stater && stater.code === "ENOENT")
fs$rename(from, to, CB);
else
cb(er)
})
}, backoff)*/
if (backoff < 100)
backoff += 10;
return;
}
if (cb) cb(er)
})
}})(fs.rename)
}
回答by Srivathsa Harish Venkataramana
I have had this issue multiple times only in Windows I try these in the order usually.
我仅在 Windows 中多次遇到此问题,我通常按顺序尝试这些问题。
npm install --force- Check if node_modules is set to read-only and remove if it is
- Delete
node_modules/ - Check if any editor is opened that could have access to the root folder of the project
- Reboot :(
npm install --force- 检查 node_modules 是否设置为只读,如果是则删除
- 删除
node_modules/ - 检查是否打开了任何可以访问项目根文件夹的编辑器
- 重启 :(
Usually trying npm installafter one of those steps will resolve it.
通常npm install在这些步骤之一之后尝试将解决它。

