Disable npm cache

半腔热情 提交于 2020-12-29 05:15:18

问题


Some time ago I had trouble with the npm cache on our build machines. From time to time we had to do npm cache clean by hand, and that solved various issues that we are still not certain about what caused them. So after a time we included npm cache clean in all our build scripts, since then we did not have mysterious problems with npm i, however now parallel builds obviously affect each other.

For me the best solution seems to be completely turn off the npm caching mechanism, but I couldn't find out how to do that.


回答1:


You could fix the problem with parallel builds by creating a new directory for one series of npm commands and set its cache to that empty directory and then remove that directory afterwards. Like:

export npm_config_cache=$(mktemp -d) 
npm ...
...
rm -rf $npm_config_cache

This would remove the need for npm cache clean as it would always start out with an empty cache.




回答2:


As npm-config documented:

force§ Default: false Type: Boolean Makes various commands more forceful.

  1. lifecycle script failure does not block progress.
  2. publishing clobbers previously published versions.
  3. skips cache when requesting from the registry.
  4. prevents checks against clobbering non-npm files.

Maybe use -f / --force is the simplest way to disable npm cache.

npm install --force



回答3:


In my case, i was facing similar issue while running multiple instance of 'npm install' on VM used for build(Windows)

Since it was a VM used only for build there was no other program locking the files. I tried disabling various antivirus settings which didn't worked. "npm cache clear" and "npm cache verify" worked but it was not a right solution for me as i cannot guess when somebody will trigger a build job from Jenkins for different release/environment leading to multiple instance of 'npm install' and hence i cannot add it to the build script nor i can go login to VM and clear/delete the cache folders manually every time.

Finally, after some research, I ended up running "npm install" with separate cache path for each job using following command:

npm install --cache path/to/some/folder

Since, all the jobs running at the same time now had a separate cache path rather than the common global path (Users/AppData/Roaming/), this issue got fixed as the jobs were no more trying to lock and access the same file, from the common npm cache.

Please note you can install a single package with a cache path as follows:

npm install packageName --cache path/to/some/folder

I was not able to find this way of giving a cache path in npm documentation but i gave it a try and it worked. I am using npm6 and looks like it works since npm5 as @ArchNoob mentioned in the comment



来源:https://stackoverflow.com/questions/36155072/disable-npm-cache

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!