
Yarn is a package management tool released by facebook that replaces npm and
Yarn caches each downloaded package, so there is no need to download it again when using it again. At the same time, parallel downloads are utilized to maximize resource utilization, so the installation is faster.Yarn will algorithmically verify the integrity of each installation package.Yarn can ensure the same work on different systems Use npm install -g to install the yarn package management tool globally. The default installed version is yarn 1 .
# Global installation npm install -g yarn # Check the yran installation version yarn --version #Display command list yarn help2-2 Project installation
You need to use yarn 2 in the project. You can install 333 in the project directory.
“Berry” is the codename for the Yarn 2 release sequence, and is also the name of our code repository!
yarn set version berry2-3 yarn update
Update yarn to the latest version, yarn will download the latest binaries from our website and install them in your project
Upgrade the package management tool in the project to
Yarn 2If you need to upgradeYarn 2later, you can useyarn set version latestto upgrade. Otherwise, you will still operateYarn 1
yarn set version latest2-4 Install the latest version of master branch
Try the latest master code branch
yarn set version from sources
You can use the --branch parameter to specify a specific branch node to install.
yarn set version from sources --branch 1211
3-1 Install Taobao image
Modifying domestic images can speed up software package installation
View currently used images
yarn config get registry
Add yarn ’s Taobao mirror
yarn config set registry https://registry.npm.taobao.org -g #Restore default yarn config set registry http://registry.npmjs.org/ # Install sass yarn config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/3-2 yrm image management
yrm is a tool for managing images. It can list the images that can be used, which is very convenient.
Install yrm
npm install -g yrm
List available images
yrmls
Use Taobao mirror
yrm use taobao
Test mirror speed
yrm test taobao
yarn init is used to initialize and generate a new package.json file
D:My Study 8-Node.js 2-yarn>yarn init yarn init v1.22.19 question name (02-yarn): yarn-init question version (1.0.0): question description: Initial configuration question entry point (index.js): question repository url: question author (jsx <[email protected]> (https://github.com/xiaofeilalala)): question license (MIT): private question: success Saved package.json Done in 29.32s.
{
"name": "yarn-init",
"version": "1.0.0",
"description": "Initialization configuration",
"main": "index.js",
"author": "jsx <[email protected]> (https://github.com/xiaofeilalala)",
"license": "MIT"
} 4-2 Set configuration items Use yarn config to set, display, and delete configuration items.
yarn config list //Display all configuration items yarn config get <key> //Display a certain configuration item yarn config delete <key> //Delete a certain configuration item yarn config set <key> <value> [-g|--global] //Set configuration items4-3 Install dependencies
Install all dependencies
yarn install
Force re-download of all packages
yarn install --force
Adding dependencies will automatically update the package.json and yarn.lock files.
#Install the latest version yarn add [packageName] #Install the specified version yarn add [packageName]@<version> #Install the specified tag version beta, next or latest yarn add [packageName]@<tag>
The exact version of the installation package, for example: yarn add [email protected] will accept version 1.9.1 , but yarn add [email protected] --exact can only install the specified version 1.2.3
yarn add [packageName]@<version> --exact yarn add [packageName]@<version> -E
Install the latest minor version of the package, for example: yarn add [email protected] --title will accept 1.2.9 but not 1.3.0
yarn add [packageName]@<version> --title yarn add [packageName]@<version> -T4-4 Different dependency classes
In a Node.js project, package.json is almost a necessary file. Its main function is to manage the external dependency packages used in the project. It is also the entry file for the npm command.
npm currently supports the following types of dependency package management:
dependenciesdevDependenciespeerDependenciesoptionalDependenciesbundledDependencies / bundleDependencies dependencies
Application dependencies, or business dependencies, are our most commonly used dependency package management objects! It is used to specify the external packages that the application depends on. These dependencies are required for normal execution after the application is released, but do not include packages used during testing or local packaging.
devDependencies
Development environment dependencies are second only to dependencies in usage frequency! Its object definition is the same as dependencies , except that the packages in it are only used in the development environment, not in the production environment. These packages are usually unit tests or packaging tools, such as gulp , grunt , webpack , moca , coffee , etc.
peerDependencies
Equivalent dependencies, or companion dependencies, are used to specify host versions that are compatible with the current package (that is, the package you wrote). How to understand it? Just imagine, we write a gulp plug-in, but gulp has multiple main versions. We only want to be compatible with the latest version. At this time, we can use peerDependencies to specify
optionalDependencies
Optional dependencies, if there are some dependent packages that the project can still run even if the installation fails or you want npm to continue running, you can use optionalDependencies . In addition, optionalDependencies will overwrite the dependency package with the same name in dependencies , so do not write it in both places.
bundledDependencies / bundleDependencies
Bundling dependencies, bundledDependencies is an array object containing dependent package names. When publishing, the packages in this object will be packaged into the final release package.
If you do not specify a dependency type, it will be installed in dependencies by default. You can also specify the dependency type.
# Add to devDependencies dependencies yarn add [package]@[version] --dev yarn add [package]@[version] -D # Add to peerDependencies dependencies yarn add [package]@[version] --peer yarn add [package]@[version] -P # Add to optionalDependencies dependencies yarn add [package]@[version] --optional yarn add [package]@[version] -O4-5 Upgrade dependencies
Upgrade the installed dependency packages as needed
# Update all packages yarn up # Upgrade to the latest version yarn up [packageName] # Upgrade to the specified version yarn up [packageName]@[version] # Upgrade to the specified tag version yarn up [packageName]@[tag]4-6 Delete dependencies
Deleting dependencies from the project will automatically update package.json and yarn.lock
yarn remove [packageName]
Delete yarn global package
yarn remove -g [packageName]4-7 Release module
yarn publish is used to publish the current module to http://npmjs.com
If you have already registered, use the following command to log in
yarn login
Log out of npm warehouse
yarn logout
After logging in, you can use the npm publish command to publish
yarn publish
Unpublish a module npm unpublish
# Delete a version yarn unpublish [packageName]@<version> # Delete the entire npm market package yarn unpublish [packageName] --force4-8 Run command
yarn run is used to execute scripts defined under scripts attribute in package.json
// package.json
{
"scripts": {
"dev": "node app.js",
"start": "node app.js"
}
} Like npm yarn can have two abbreviated ways of running scripts: yarn start and yarn test
# yarn executes the script node app.js corresponding to dev yarn run dev npm run yarn start # yarn npm start # npm4-9 Cache Control
List each package that has been cached
yarn cache list
global cache location
yarn cache dir
clear cache
yarn cache clean4-10 Module information
yarn info can be used to view the latest version information of a module
yarn info [packageName] # yarn npm info [packageName] # npm yarn info [packageName] --json # Output json format npm info [packageName] --json # npm yarn info [packageName] readme # Output README part npm info [packageName] readme