What does yarn --prefer-offline do?

I am assuming that when I install the npm package, tell me first time to respond with

yarn add react

      

this will save the react file to the local cache. I found it .yarn-cache

contains a lot of files. I am assuming this is a direct local cache folder, so when I install a new react in the future, it will be installed from the local cache, no ??

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

yarn add react

      

or

yarn add react --prefer-offline

      

?

+3


source to share


3 answers


I understand that by default yarn will always try to download the package from the internet when installing / restoring it and will also store it in the cache, which means that in the future if you try to install / restore and have no internet connection it might go back to cache and install there if necessary. By specifying --prefer-offline

, you change this behavior to check the cache first and try to download the package from the internet if it can't find it in the cache. This can speed up the installation / repair process significantly and allow repeated builds, but you cannot get the latest versions (for example, if you are using version specs like ~ 1.2.3). There is also a parameter--offline

which throws an error if it cannot find the package in your local cache (i.e. it will never try to download from the internet).



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

+7


source


To use --prefer-offline

, you first need to set up an offline batch repo.

Let's set up our cache in a hidden directory in the home folder:

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

      

Also set the config to have yarns flush downloaded tarballs:

yarn config set yarn-offline-mirror-pruning true

      

Now, whenever you run yarn install

in any project, it will cache the modules in that directory available to you to then retrieve with yarn --prefer-offline

.

If you want to later, perhaps in a new project, install from the cache, you will need to specify the desired version of the module, as it has no idea latest

. The easiest way is to simply 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...

      

Then I can install the latest version offline with

yarn add moment@2.19.3

      

The yarn post blog mentioned by @adrian details how to cache a project and how to do it for your team, if needed. I myself only use one cache in order to be able to download new projects offline.

+2


source


A very popular guy here on SO said:

"Read the Source, Luke!"

And here is the yarn

CLI source--prefer-offline

:

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

      

Enjoy!

-3


source







All Articles