Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

npm install fails on Windows with "EPERM, operation not permitted" error during rename #19

Closed
izuzak opened this issue Dec 23, 2012 · 34 comments · Fixed by #28
Closed

Comments

@izuzak
Copy link
Contributor

izuzak commented Dec 23, 2012

Doing a npm install phantomjs on a Win7 machine is giving me the error below. Looks like everything works up until the very end, when the install script breaks on renaming the phantomjs dir?

I'm running the command under an admin account and I've also tried the "windows key -> type cmd -> right click on cmd icon and Run as administrator" trick but get the same error. I also checked that I have the required privileges on the directories.

Never had problems with installing any npm package before on the same machine.

Any ideas? The ENOTEMPTY error code suggests that the underlying cause is renaming an non-empty dir? 😕

Thanks!

...
Recieved 6605K total.
Extracting zip contents
Read stream closed
Renaming extracted folder phantomjs-1.7.0-windows  -> phantom

fs.js:439
  return binding.rename(pathModule._makeLong(oldPath),
                 ^
Error: EPERM, operation not permitted 'C:\...\noam\node_modules\phantomjs\tmp\phantomjs-1.7.0-windows'
    at Object.fs.renameSync (fs.js:439:18)
    at Extract.finishIt (C:\...\noam\node_modules\phantomjs\install.js:73:12)
    at Extract.EventEmitter.emit (events.js:123:20)
    at ProxyWriter.<anonymous> (C:\...\noam\node_modules\phantomjs\node_modules\unzip\lib\extract.js:34:10)
    at ProxyWriter.EventEmitter.emit (events.js:90:17)
    at DirWriter.EventEmitter.emit (events.js:90:17)
    at end (C:\...\noam\node_modules\phantomjs\node_modules\unzip\node_modules\fstream\lib\writer.js:323:12)
    at Writer._finish (C:\...\noam\node_modules\phantomjs\node_modules\unzip\node_modules\fstream\lib\writer.js:313:32)
    at endChmod (C:\...\noam\node_modules\phantomjs\node_modules\unzip\node_modules\fstream\lib\writer.js:185:44)
    at setProps (C:\...\noam\node_modules\phantomjs\node_modules\unzip\node_modules\fstream\lib\writer.js:293:5)
npm ERR! error rolling back Error: ENOTEMPTY, rmdir 'C:\...\noam\node_modules\phantomjs\tmp\phantomjs-1.7.0-windows'
npm ERR! error rolling back  phantomjs@0.2.6 { [Error: ENOTEMPTY, rmdir 'C:\...\noam\node_modules\phantomjs\tmp\phantomjs-1.7.0-windows']
npm ERR! error rolling back   errno: 53,
npm ERR! error rolling back   code: 'ENOTEMPTY',
npm ERR! error rolling back   path: 'C:\\...\\noam\\node_modules\\phantomjs\\tmp\\phantomjs-1.7.0-windows' }
npm ERR! phantomjs@0.2.6 install: `node install.js`
npm ERR! `cmd "/c" "node install.js"` failed with 1
npm ERR!
npm ERR! Failed at the phantomjs@0.2.6 install script.
npm ERR! This is most likely a problem with the phantomjs package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node install.js
npm ERR! You can get their info via:
npm ERR!     npm owner ls phantomjs
npm ERR! There is likely additional logging output above.

npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "phantomjs"
npm ERR! cwd C:\...\noam
npm ERR! node -v v0.8.11
npm ERR! npm -v 1.1.62
npm ERR! code ELIFECYCLE
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\...\noam\npm-debug.log
npm ERR! not ok code 0
@dpup
Copy link
Contributor

dpup commented Dec 23, 2012

I won't have access to a windows machine until after the holidays, will be happy to review patches though.

@izuzak
Copy link
Contributor Author

izuzak commented Dec 23, 2012

This node issue (fixed) looks very related: nodejs/node-v0.x-archive#3051

Will try to understand what's causing this.

@izuzak
Copy link
Contributor Author

izuzak commented Dec 24, 2012

I've fiddled around with the project's install script. Something "strange" happens during the extract->rename process -- if renaming is done immediately or soon after extracting - the error shows its ugly head. I.e. even if I call finishIt() 10-20-30 secs after extracting -- renaming still fails. However, if a bit more time passes (e.g. 60 secs) - renaming succeeds. Looks like the file descriptors of the extracted files are not being released so renaming fails until they are (by a timeout?). Not sure yet if this is a problem of unzip or the underlying fstream library.

@skotchio
Copy link

I have the same issue on my Windows 7 x64 machine.

@sindresorhus
Copy link

Is this still a problem with Node 0.8.19?

@skotchio
Copy link

Yes. unfortunately the same error.

@dpup
Copy link
Contributor

dpup commented Feb 21, 2013

My windows machine died and I'm not replacing it. If someone wishes to debug and patch that'd be awesome.

@izuzak
Copy link
Contributor Author

izuzak commented Feb 23, 2013

I gave it another shot, but didn't get much further than the last time.

Here's what I "have": if finishIt() doesn't run immediately after extraction (this is the function doing the renaming), but with a 10sec delay - it is possible that the error will not show up. Why "possible"? Because sometimes it does, and sometimes it doesn't. The longer the delay in executing finishIt(), the higher the probability that the error will not show up. If I try manually deleting the extracted folder immediately after extraction (through windows explorer) - the error manifests as the "You need permission to perform this action. You require permission from Administrators to make changes to this folder." This is strange since my user account is indeed in the Administrators group. And if I keep trying to delete the file, after some time it just succeeds.

So, this suggests that it really is an fs ownership issue, but not sure where to go from here. The strangest thing is the nondeterminism in the appearance of the error -- as if there is some randomness as to when the ownership of the folder magically resolves itself.

At first I thought that the finishiIt() function was getting called before all extracted files were closed by fstream, but after carefully logging everything - I found that this was not the case and finishIt() is called after all extracted files are closed by fstream.

Anyone know how to track low-level fs events that might indicate what changes in the permissions of the extracted files and when?

@stevenbenner
Copy link

I was also experiencing this issue, I do believe the culprit is the unzip library and it's streams, but I haven't been able to confirm that yet. Though I was able to confirm that switching from unzip to adm-zip fixed the issue on my system.

So I've submitted pull request #27. I'd appreciate it if any of you could test my code and verify that the change fixes the issue for you (and doesn't cause any others).

@izuzak
Copy link
Contributor Author

izuzak commented Feb 23, 2013

Hey Steve! Hate to say this but -- I have the same OS, npm and node version as you do, but using your fix with adm-zip does not fix the problem on my machine. I get the same exact error. :(

However (!), this made me rethink if this even is an issue with node.js -- and I believe I've found the source of the problem. It's our beloved anti-virus software. Yes. You know it and you're sighing and shaking your head just as I have :). What's worse is that someone else reported a similar issue for npm here: https://github.com/isaacs/npm/issues/2288 and pointed to a fix here:
isaacs/npm@beeb7fd. Strange that I haven't noticed this before.

Anyway, on windows machines, we just need to re-try renaming until the EPERM error stops showing up, which happens after the AV software stops scanning the new files.

Needless to say - I've applied a similar fix to the project (the one which uses unzip) and now everything works fine (I've tried it a dozen times). 🍰

Pull request with the patch incoming shorty.

@stevenbenner
Copy link

Oh my god. That is it after all. I disabled my anti-virus software (NOD32 in my case) and retried the original code. It installed flawlessly on the first go. That is just, wow.

Alright, well at least you figured it out before I wasted a bunch of people's time. I'll withdraw my pull request, looking forward to seeing yours go out soon!

@izuzak
Copy link
Contributor Author

izuzak commented Feb 23, 2013

@stevenbenner I would have not thought of the A/V stuff without you trying the adm-zip fix. So, hats off to you too, sir :)

@KevinGaudin
Copy link

I can confirm that the error vanishes after disabling A/V software. (Panda Cloud here)

@hunternovak
Copy link

Nice catch. Just hit this issue as well and this thread saved me some serious time. Cheers!

@izuzak izuzak closed this as completed Mar 14, 2013
@danielkcz
Copy link

@erperejildo
Copy link

Thank you FredyC!!! 🎯

@ryepup
Copy link

ryepup commented Oct 21, 2014

I had this problem today with Windows Server 2012R2, npm v1.3.24, and node v0.10.25.

In my case, it was a slightly different backtrace starting with an lstat. My CI server was running as a local user "Jenkins", and I needed to give that read-only access to the root drive letter where I was trying to npm install things.

icacls d: /grant Jenkins:(R)

Then it worked ok, so probably a quirk of windows permissions, or something is walking up the filesystem folder hierarchy looking for a config file.

@ghost
Copy link

ghost commented Feb 12, 2015

I ran in to the same problem.

Dev system:

  • W7 Ultimate (x64)
  • Node.js 0.10.32
  • npm 2.5.0
  • phantomjs@1.9.15
  • AVG AntiVirus free
  • using GitBash command tool (git version 1.9.4.msysgit.2)

Command: $ npm install

Local project with package.json and npm-shrinkwrap.json where phantomjs is a dependency of grunt-html-snapshot@0.6.1 (html-snapshots => same prob).

Actually I did not ran into this problem, but my co-worker did who has an exact setup to mine.
I have this issue 1 out of 100, co-worker 100 out of 100 attempts.

Turning off AVG, windows defender or windows firewall has no effect.

These are the exact steps I did on my co-workers system:

  • Open the node.js command prompt (Windows button => all programs => node.js => node.js command prompt => run as administrator)
  • $ npm -g install npm@2.5.0
  • Now I can run $ npm install on the project folder using GitBash (not executed as administrator), and it works without any problems.

My opinion:
It is probably enough to just run node.js command prompt once, maybe even without entering a command.

Seems that when you never ran the node.js command prompt, the system seems to think that it is unsafe for cmd or GitBash to run an exe (phantomjs.exe) through node.js.

Hope this helps someone out.

@radum
Copy link

radum commented Feb 19, 2015

@BohemMedia WTF I just did what you said running the node cmd prompt only once and now all my 99 problems are gone. Crazy. So I can confirm this works for me. (Win 7 x64 node 0.12.0)

@musicmen
Copy link

i just ran into the same problem on win 7 x64 and what shoudl i say: turnig off commodo antivirus while installing fixed the issue, thanks a lot!

@KuhnPan
Copy link

KuhnPan commented Mar 13, 2015

@izuzak wow!! It works after disable AV software, you really save our times, thanks vvvvvvvvvvery much! :-)

@ventuc
Copy link

ventuc commented Apr 29, 2015

Wow, I had the same issue. Simply running NodeJs command prompt as administrator fixed it. I didn't need to execute any command from Node.js cmd prompt, I've only launched it, exactly as @BohemMedia said. Thanks!

@douglascorrea
Copy link

I had the same issue even using command prompt as administrator the problem happened. After stopping Panda AntiVirus the problem was solved

@mokanfar
Copy link

mokanfar commented Aug 1, 2015

What if you cannot disable antivirus because its running as a service on a network?

@nicks
Copy link
Contributor

nicks commented Aug 3, 2015

@mokanfar per the faq, you can put phantomjs on PATH manually:
https://github.com/Medium/phantomjs#i-tried-everything-but-my-network-is-b0rked-what-do-i-do

@astroanu
Copy link

@BohemMedia is correct. run npm command prompt once. then you can use npm from the git bash without issues.

@Loubot
Copy link

Loubot commented Nov 11, 2015

On windows
npm cache clean
worked for me

@cnaccio
Copy link

cnaccio commented Nov 14, 2015

installing npm globally as admin, and then running npm cache clean fixed my problem. Thanks guys!

@Falasarabeatle
Copy link

Hi, please, could anyone explain to me how to install hospitalrun? I am a physician from Venezuela and I do need this software for my patients. I have no idea how to program. This is my e-mail, falasarabeatle@hotmail.com. thanks

@emanb29
Copy link

emanb29 commented Dec 21, 2015

+1 for suspending antivirus (Kapersky Internet Security 2016) in my case. Possibly adding an exception should work too

@shawmanz32na
Copy link

I'd like to report that after clearing the npm cache, using --force, running in the node.js command prompt, AND disabling Windows Defender (my only anti-virus), I am still encountering this error.

per npm/npm#9696 , this may be an NPM bug exacerbated by slow windows machines.

@feseha
Copy link

feseha commented Jan 13, 2016

There is a fix for this on github!
I have a system that emits the following with ember -v
C:\Users\Feseha>ember -v
version: 1.13.14
node: 5.4.1
npm: 2.14.10
os: win32 x64

To fix the problem, I followed the suggestion at:
npm/npm@beeb7fd

Specifically:

  • I fired up TextPad as an administrator,
  • deleted lines 229-242 (that is the "function unpack_" block)
  • replaced them with the code below.
  • tested it with ember new super-rentals and it completed without a hitch.
    Feseha

function unpack_ ( tarball, unpackTarget, dMode, fMode, uid, gid, cb ) {
rm(unpackTarget, function (er) {
if (er) return cb(er)
log.verbose(unpackTarget, "rm'ed")
moveIntoPlace(folder, unpackTarget, function (er) {
if (er) return cb(er)
log.verbose([folder, unpackTarget], "renamed")
// curse you, nfs! It will lie and tell you that the
function unpack_ ( tarball, unpackTarget, dMode, fMode, uid, gid, cb ) {
})
}

function moveIntoPlace (folder, unpackTarget, cb) {
var start = Date.now()
fs.rename(folder, unpackTarget, function CB (er) {
if (er
&& process.platform === "win32"
&& er.code === "EACCES"
&& Date.now() - start < 1000) {
return fs.rename(folder, unpackTarget, CB)
}
cb(er)
})
}

@zn-fe
Copy link

zn-fe commented Jan 18, 2016

close all other software may open this directory or sub-directory here

@Fiyinf
Copy link

Fiyinf commented Feb 10, 2016

Running nodejs command prompt as administrator worked perfectly! Thank you!

@Medium Medium locked and limited conversation to collaborators Feb 10, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.