because
YarnThere is a function of offline installation, especially in the domestic network environment is not good, this function is particularly useful.

The problem

One of the situations we often encounter is: You have set up your network, installed all dependencies smoothly, and written package.json. NPM automatically generates a package-lock.json for you. In order to ensure consistency, NPM has specified the network download path of each package in package-lock.json. However, your classmate’s network environment is not as good as yours, so what you can successfully download cannot be downloaded by NPM. As a result, it takes a long time to set up this network. Although there are alternatives like CNPM in China, there are also various strange phenomena from time to time, which are not as smooth as the authentic NPM.

Yarn

To solve this problem once and for all, we need Yarn.

The initial installation of Yarn is simple:

brew install yarnCopy the code

Execution is simpler:

yarnCopy the code

In this case, it generates a yarn.lock file, which is similar to the package-lock.json file generated by NPM and contains the network download path of each dependent package (in fact, NPM borrowed the concept of package-lock.json from YARN). You can then run YARN Start to start the development environment, or yarn Run build to do the compilation. Yarn can do whatever NPM can do. So far, there seems to be nothing magical about YARN.

offline

Now let’s set up the offline installation environment for YARN, which is the best part of YARN.

In order to have a place to store the yarn downloaded installation packages, we need to set up a folder that is completely independent of the project and machine. Assuming that we store these files in the root directory of the computer, we first create a folder and allow others to access it:

sudo mkdir /Projects
sudo chmod 777 /ProjectsCopy the code

We then tell YARN to go here to store and read the offline installation package:

yarn config set yarn-offline-mirror /Projects/yarn-offline-mirrorCopy the code

At this point, yarn will generate a.yarnrc file in your current user’s root directory. You can move this file to your project’s root directory:

mv ~/.yarnrc ./Copy the code

The content of this file is very short, open to take a look:

# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


lastUpdateCheck 1520049128107
yarn-offline-mirror "/Projects/yarn-offline-mirror"Copy the code

The general idea is that this is an auto-generated file, and you shouldn’t modify it directly (although it doesn’t matter if you know what you’re doing, it’s not a binary file). This is where we store our offline packages.

Next we start filling our offline folder with content. Let’s delete the node_modules folder and yarn.lock file of our current project:

rm -rf node_modules/ yarn.lockCopy the code

And then, let’s do it

yarn installCopy the code

At this point, you will see that yarn not only regenerates the node_modules folder and yarn.lock file, but also puts the downloaded installation package into the offline installation path /Projects/yarn-offline-mirror that we set up earlier.

validation

How do we verify that YARN can be installed offline? Let’s delete the node_modules folder first, and don’t delete the yarn.lock file this time:

rm -rf node_modulesCopy the code

To be thorough, let’s delete the YARN cache as well:

yarn cache cleanCopy the code

Then, we turn off the wifi, unplug the cable, and then we execute:

yarn install -offlineCopy the code

Success! Without any network, we can continue to rebuild the node_modules folder, which takes the contents of /Projects/yarn-offline-mirror that we just set up.

meaning

So what does that mean? This is not enough. We need to upload the entire yarn-offline-mirror to our own Git repository and create a separate repository for it so that multiple projects can share the same yarn-offline-mirror without wasting too much space. In addition, it is in our own file system. If yarn install-offline is executed on the server or another colleague’s computer, it will not visit the official website of NPM to obtain the installation package. After all, there is a high probability of access failure in that way.

Why don’t I just put the entire node_modules folder into the Git repository? Wouldn’t that have the same effect? In a sense, that’s true, but node_modules stores all of the files that have been extracted, and it’s not uncommon to have tens or even hundreds of thousands of files, so we usually ignore the node_modules folder in the.gitignore file when we’re building a project. Yarn-offline-mirror stores the installation package itself, which is uncompressed. TGZ files. Generally, dozens or even hundreds of files are enough for a project, which is also a good thing for our git server management, so we can put it in Git to manage.

reference

You can take a closer look at the official introduction, if you’re interested.

Finally, I hope you can be happy!