What does yarn --prefer-offline do?

前端 未结 3 2025
旧巷少年郎
旧巷少年郎 2021-02-20 05:19

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 .y

3条回答
  •  遥遥无期
    2021-02-20 05:33

    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.

提交回复
热议问题