npm install step in my Teamcity CI build for an angular app I have been working has been failing intermittently and I finally uncovered the reason.
TL/DR The combination of McAfee Anti-virus and network mounted user AppData folders was the culprit - moving them to an unscanned local folder fixed it.
npm install was failing the build intermittently when run by our build software Teamcity on a windows agent with ugly errors like:
npm ERR! Error: EPERM: operation not permitted rename 'C:\Users\the-build-user\AppData\Roaming\npm-cache\wrappy\1.0.2\package\package.json.363dae6043e3d4d0bd28d6ad9849acf4'
Sometimes a simple cache clear like the below would fix it, but other times it was more stubborn.
npm cache clear
I turns out that the default install location for npm is in the running users AppData folder. There are two folders, one for npm prefix folder which is the global install location and another for a cache of packages retrieved from the internet. The default paths are based on the
%AppData%\Roaming path so typically end up as:
In my work windows server environment the users AppData folder is actually on a network share under the hood so it follows you around from machine to machine, although to users it appears as a regular folder. It was also monitored by the anti-virus scanner, in this case McAfee, and those folders were not on any scanning exclusion lists.
The combination of network location and McAfee scanning disagreed with node.js and npm meaning that files would appear locked or missing and often generate the not permitted error.
The solution was to move the files to a new location and change the users configuration to use the new paths. It also sped up the builds which was a nice perk!
First move the files to a real 'local' path that is ignored by the AV software, in my case e:\buildagent, and then change the configuration
"Create new folders" mkdir E:\Buildagent\npm mkdir E:\Buildagent\npm-cache "move npm prefix and the cache" 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 "Update the npm config for prefix and cache" npm config set prefix E:\\Buildagent\\npm npm config set cache E:\\Buildagent\\npm-cache
Then you may need to change the path environment variable if %AppData%\Roaming\npm is on configured to be on your path so that it points to the new destination as set above in the prefix config setting. Either edit it manually or use the excellent RapidEE for the task.
Finally test that npm install still works. With any luck your build should be reliable again.