I started on Monday, and my colleague JJ let me familiarize myself with the new project based on React. In the past, my steps have been:
git clone xxx
npm install
npm run dev
Copy the code
At this point, JJ gave me the following paragraph
git clone xxx
yarn
yarn start
Copy the code
“Gee, what the hell is Yarn? Is a more advanced alternative to NPM? Why replace NPM? Is there any good?” “A series of inner questions came up. I asked JJ silently, “Is YARN the same thing as NPM?” “Yes.” JJ was busy tapping away, and it was clear that the question wasn’t worth asking. I also silently put down a series of questions in my mind just now.
What is Yarn?
“Yarn is a new JS package management tool from Facebook, Google, Exponent, and Tilde. As the official documentation states, Yarn was created to address some of the shortcomings of NPM.” This one reminds me of the pitfalls of using NPM:
npm install
whenSlowing the. In particular, it takes a long time to pull down new projects, delete node_modules, and reinstall them.- The same project cannot be installed consistently. Because of the nature of the version numbers in package.json files, the following three version numbers mean different things during installation.
"5.0.3", "~ 5.0.3", "^ 5.0.3." "Copy the code
“5.0.3” indicates that the specified 5.0.3 version is to be installed. “~ 5.0.3” indicates that the latest version in 5.0.x is to be installed. “^5.0.3” indicates that the latest version in 5.x. X is to be installed. This is the trouble, often will appear the same project, some colleagues are OK, some colleagues will be due to the installation of the version of the inconsistent bug.
- During installation, packages are downloaded and installed at the same time. At some point, a package throws an error, but NPM continues to download and install packages. Because NPM will output all the logs to the terminal, the error information about the error packet will be lost in the pile of warnings printed by NPM, and you will never even notice the actual error occurred.
With these pits in mind, I began to understand the advantages of Yarn and the problems it solves.
Advantages of Yarn?
- Speed is fast. The fast speed mainly comes from the following two aspects:
- Parallel installation: Both NPM and Yarn perform a series of tasks during package installation. NPM executes each package in a queue, which means you must wait until the current package is installed before proceeding with subsequent installations. Yarn synchronizes all tasks, improving performance.
- Offline mode: If a software package has been installed before, obtain it from the cache when Yarn is used to install it again. You do not need to download it from the network like NPM.
- Unified installation version: To prevent different versions from being pulled, Yarn has a lock file that records the versions of exactly installed modules. Yarn creates (or updates) the yarn.lock file every time a new module is added. This ensures that the same module version is used each time the same project dependency is pulled. NPM has a way to use the same version of packages everywhere, but you need to run the NPM shrinkwrap command. This command will generate a lock file that will be read before NPM install is executed, just as Yarn reads yarn.lock. The difference between NPM and Yarn is that Yarn generates the lock file by default. NPM uses the shrinkwrap command to generate the npm-shrinkwrap. Packages version information is logged and updated.
- More concise output: The output of NPM is verbose. When NPM install is executed, the command line will constantly print out all dependencies that are installed. By contrast, Yarn is much simpler: by default, it combines emoji to print out necessary information intuitively and directly. It also provides commands for developers to query additional installation information.
- ** Multiple registry source handling: ** All dependencies, no matter how many times they are indirectly referenced by different libraries, are installed from only one registry source, either NPM or Bower, to prevent confusion and inconsistency.
- Better semantics: Yarn has changed the names of some NPM commands, such as yarn add/remove, which is clearer than the original NPM install/uninstall.
Contrast the Yarn and NPM commands
npm | yarn |
---|---|
npm install | yarn |
npm install react –save | yarn add react |
npm uninstall react –save | yarn remove react |
npm install react –save-dev | yarn add react –dev |
npm update –save | yarn upgrade |
The future of NPM: NPM5.0
With the pressure of YARN, NPM made some similar improvements.
- By default, package-lock.json like yarn.lock is added.
- Git dependency support optimization: This feature is useful when you need to install a large number of internal projects (for example, developing on an Intranet without self-built sources), or when you need to use unreleased versions of certain dependencies. Until then, you may need to specify commit_id to control the version.
- File dependency optimization: In previous versions, if a local directory was installed as a dependency, the file directory would be copied to node_modules. In NPM5, instead of copying files, you will create symlinks (with the exception of using a local tarball package). This will speed up the installation. Yarn does not support this function.
conclusion
Before NPM5.0, YARN had a significant advantage. However, after NPM, through the above series of comparisons, we can see that NPM5 in the speed and use of a great improvement, it is worth trying, but not more than YARN.
In conclusion, my personal advice is that if you are already using YARN for your personal projects and have no more problems, you can continue to use yarn for now. However, if you have nPM-compatible scenarios, or if you are on a team that uses NPM, CNPM, or TNPM, or if you have not yet switched to YARN, now is the time to try NPM5.
Welcome to discuss, click like before you go ~
Articles are synchronized in the following community, you can choose one to follow me oh. ◕ ‿ ◕.
Simbawu | making | segmentfault | zhihu | | Jane books the Denver nuggets