Detailed explanation of common commands of Yarn

Posted by homerjsimpson on Wed, 19 Jan 2022 07:42:38 +0100

Introduction to Yarn

Yarn It is a new JavaScript package management tool developed by Facebook, Google, Exponent and Tilde. As we can see from the official documents, its purpose is to solve the few problems faced by these teams using npm.

Yan official website: https://classic.yarnpkg.com/zh-Hans

install

Official website script:

curl -o- -L https://yarnpkg.com/install.sh | bash -s -- --nightly

perhaps

npm install -g yarn

After successful installation, you can view the version:

yarn –version

Common commands

Initialize new project

yarn init

npm mode:

npm init

Like npm init, yarn init creates a package through an interactive session JSON file.

Add dependent package

Adding dependencies through yarn add will update the package JSON and yarn Lock file.

yarn add [package]yarn add [package]@[version]yarn add [package]@[tag]

npm mode:

npm install [package]

Add dependencies to different dependencies, such as devDependencies, peerDependencies, and optionaldependences:

yarn add [package] --devyarn add [package] --peeryarn add [package] --optional

Upgrade dependent package

yarn upgrade [package]yarn upgrade [package]@[version]yarn upgrade [package]@[tag]

npm mode:

npm update [package]

Remove dependent packages

yarn remove [package]

npm mode:

npm uninstall [package]

All dependencies of the installation project

yarn

perhaps

yarn installyarn install --force # Forced download and installation

npm mode:

npm installnpm install --force # Forced download and installation

Run script

yarn run [script] [<args>]

yarn run is used to execute package The script defined under the scripts attribute in JSON, for example:

{  "name": "my-package",  "scripts": {    "dev": "node app.js",    "start": "node app.js"  }}

Execute the script node app corresponding to dev under the scripts attribute js

yarn run dev

npm mode:

npm run dev

Execute the script node app corresponding to start under the scripts attribute js

yarn start

npm mode:

npm start

Display a package information

yarn info <package>

This command will pull the package information and return it in tree format, for example:

yarn info react
yarn info vx.x.x
{ name: 'react',
  version: '15.4.0-rc.2',
  description: 'React is a JavaScript library for building user interfaces.',
  time: { modified: '2016-10-06T22:09:27.397Z', ... } 
  ...
}

The default report style of this command is single quote serialization. If you want to output a valid json line format, use the standard -- json flag:

yarn info react --json
{"type":"inspect","data":{"name":"react","time":{...}}}{"type":"finished","data":417}

npm mode:

npm info <package>npm info <package> --json

Lists all dependencies of the project

yarn list

npm mode:

npm list

The list command in Yarn lists all dependencies of the current working folder by referring to the meta information files of all package managers, including project dependencies, for example:

yarn list vx.x.x
├─ package-1@1.3.3
├─ package-2@5.0.9
│  └─ package-3@^2.1.0
└─ package-3@2.7.0

By default, the dependencies of all packages will be displayed. If you want to limit the depth of dependencies, you can add a flag to the list command -- depth required:

yarn list [--depth] [--pattern]

For example:

yarn list --depth=0

Manage yarn profiles

set up:

yarn config set <key> <value> 

npm mode:

npm config set <key> <value> 

Read:

yarn config get <key>

npm mode:

npm config get <key>

Delete:

yarn config delete <key>

npm mode:

npm config delete <key>

To view the current configuration:

yarn config list

npm mode:

npm config list

Set Taobao image:

yarn config set registry https://registry.npm.taobao.org

npm mode:

npm config set registry https://registry.npm.taobao.org

cache

List cached packages:

yarn cache list

List cached packages that match the specified pattern:

yarn cache list --pattern <pattern>

For example:

yarn cache list --pattern gulp
yarn cache list --pattern "gulp|grunt"
yarn cache list --pattern "gulp-(match|newer)"

Print out the location of the current yarn global cache:

yarn cache dir

Clear cache:

yarn cache clean

In addition, you can specify one or more packages you want to purge:

yarn cache clean [<module_name...>]

Change cache path

Set cache folder to configure cache directory:

yarn config set cache-folder <path>

You can also specify the cache directory with the -- cache folder flag:

yarn <command> --cache-folder <path>

You can also use the environment variable YARN_CACHE_FOLDER specifies the cache directory:

YARN_CACHE_FOLDER=<path> yarn <command>