Yarn Introduction

Yarn is a package manager for your code. It allows you to use and share code (such as JavaScript) from developers around the world. Yarn can do this quickly, safely, and reliably, so you have nothing to worry about.

Yarn allows you to use other developers’ solutions to different problems to make your development process easier.

Code is shared through packages, or modules. A package contains all the code that needs to be shared, along with a file that describes the package information, called package.json.

1, install,

Yarn installation Please enter the portal

2. Yarn usage

Yarn is now installed and ready to use. Here are some of the most common commands you’ll need:

2.1 Initializing a new project

yarn init
Copy the code

2.2 Adding a Dependency Package

yarn add [package]
yarn add [package]@[version]
yarn add [package]@[tag]
Copy the code

2.3 Adding dependencies to different dependency categories

Add to the categories devDependencies, peerDependencies, and optionalDependencies respectively:

yarn add [package] --dev
yarn add [package] --peer
yarn add [package] --optional
Copy the code

Distinguish devDependencies, peerDependencies, and optionalDependencies

In a Node.js project, package.json is almost a must. Its main function is to manage external dependency packages used in the project, and it is also the entry file for NPM commands.

NPM currently supports the following types of dependency package management:

  • dependencies
  • devDependencies
  • peerDependencies
  • optionalDependencies
  • bundledDependencies / bundleDependencies

dependencies

Application dependencies, or business dependencies, are the most commonly used dependency package management objects! It is used to specify external packages that the application depends on, which are required for normal application execution after publication, but do not include packages used for testing or local packaging.

devDependencies

Development environment dependencies, second only to Dependencies! The object definition of Dependencies is the same as that of Dependencies, except that the package is used only in the development environment and not in the production environment. These packages are usually unit tests or packaging tools, such as Gulp, Grunt, Webpack, Moca, coffee, etc.

peerDependencies

Peer dependencies, or peer dependencies, are used to specify which host version of the current package (that is, the package you wrote) is compatible with. How do you understand that? Imagine that we write a gulp plug-in, and there are several major versions of gulp. We only want to be compatible with the latest version. We specify peerDependencies.

{
  "name": "gulp-my-plugin"."version": "0.0.1"."peerDependencies": {
    "gulp": "3.x"}}Copy the code

optionalDependencies

OptionalDependencies. You can use optionalDependencies if you have dependencies that will run even if the installation fails or if you want NPM to continue running. Also, optionalDependencies overwrites dependencies of the same name, so don’t write them in both places.

bundledDependencies / bundleDependencies

BundledDependencies. BundledDependencies are an array object containing the names of dependencies that are packaged into the final distribution.

2.4 Upgrading dependency packages

yarn upgrade [package]
yarn upgrade [package]@[version]
yarn upgrade [package]@[tag]
Copy the code

2.5 Removing a dependency Package

yarn remove [package]
Copy the code

2.6 Installing all dependencies of the project

yarn
Copy the code

or

yarn install
Copy the code

2.7 Difference between YARN and NPM

Difference between YARN and NPM