What does yarn --prefer-offline do?

久未见 提交于 2019-12-09 17:19:29

问题


I assume when I install npm package say react for the first time with

yarn add react

this will save react file in local cache. I found .yarn-cache to contain many files. I assume it is yarn local cache folder so that when I install react again in the future, it will be installed from local cache, no??

If I need to install react again in the future, should I simply write

yarn add react

or

yarn add react --prefer-offline

?


回答1:


My understanding is that by default, yarn will always try to download the package from the internet when you install/restore it, and will also store that in the cache, which means that in the future if you try to install/restore and don't have an internet connection, it can fall back on the cache and install from there if necessary. By specifying --prefer-offline, you are reversing this behaviour so that it will check the cache first, and only try to download the package from the internet if it cannot find it in the cache. This can make your install/restores significantly quicker, and will allow you perform repeatable builds, but you may not get the latest versions available (e.g. if you're using version specs like ~1.2.3). There is also an --offline option, which will throw an error if it can't find a package in your local cache (i.e. it won't ever try to download from the internet).

More info at https://yarnpkg.com/blog/2016/11/24/offline-mirror/




回答2:


In order to use --prefer-offline you first have to setup your offline package repo.

Let's setup our cache in a hidden dir in the home folder:

yarn config set yarn-offline-mirror ./.npm-offline  

Also set a config to have yarn clean the dowloaded tarballs:

yarn config set yarn-offline-mirror-pruning true

Now, whenever you run yarn install in some project, it will cache the modules in this directory, available for you to then fetch using yarn --prefer-offline.

When you want to later, perhaps in a new project, install from the cache you will need to specify the desired module version as it doesn't have a concept of latest. Easiest is to simply try to add:

yarn add moment

On my machine this prints:

error Couldn't find any versions for "moment" that matches "latest" in our cache. 
Possible versions: "2.1.0, 2.13.0, 2.17.0, 2.17.1, 2.18.1, 2.19.1, 2.19.2, 2.19.3, 2.8.4" 
// Note that above is not in semver order...

I can then install latest offline with:

yarn add moment@2.19.3

The Yarn blog post mentioned by @adrian elaborates on how to a per project cache and how to commit that for your team if desired. I myself use just one cache for in order to be ideally able to bootstrap new projects while offline.




回答3:


A quite popular guy here at S.O. said:

"Read the Source, Luke!"

And here is the source of yarn CLI's --prefer-offline flag:

commander.option('--prefer-offline', 'use network only if dependencies are not available in local cache');

Enjoy!



来源:https://stackoverflow.com/questions/43709240/what-does-yarn-prefer-offline-do

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