Yarn installation and use tutorial

Yarn installation and use tutorial

1, Official website guidelines

Quick start | Yan Chinese documents

npm Chinese document | npm Chinese website (npmshell.cn)

2, Installation

Stable version: v1.22.17

Supported Node versions: ^ 4.8.0 | ^ 5.7.0 | ^ 6.2.2 | > = 8.0.0

windows installation: Introduction to official website installation

npm install --global yarn

Yan Taobao source installation, copy and paste the following code lines to the black window to run

  • yarn config set registry https://registry.npm.taobao.org/ -g

  • yarn config set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass/ -g

Test whether Yarn is successfully installed through the following command:

yarn --version

3, Common commands

[1] Initialize new project

yarn init

[2] Add dependent package

yarn add [package] // The latest version will be automatically installed and the specified version number will be overwritten
yarn add [package] [package] [package] // Add multiple packages at once
yarn add [package]@[version] // Add a package of the specified version
yarn add [package]@[tag] // Install a tag (such as beta,next or latest)

[3] Add dependencies to different dependency categories

The dependency type is not specified. It is installed in dependencies by default and added to devdependences, peerDependencies and optionalDependencies respectively:

yarn add [package] --dev or yarn add [package] -D // Add to devdependences
yarn add [package] --peer or yarn add [package] -P // Add to peer dependencies
yarn add [package] --optional or yarn add [package] -O // Add to optional dependencies

[4] Upgrade dependent package

yarn upgrade [package] // Upgrade to the latest version
yarn upgrade [package]@[version] // Upgrade to the specified version
yarn upgrade [package]@[tag] // Upgrade to the specified tag

[5] Remove dependent packages

yarn remove [package] // Remove package

[6] Install package Package dependencies in SHELLON, and save the package and all its dependencies into yarn lock

yarn or yarn install // Install all dependencies
yarn install --flat // Install a single version of a package
yarn install --force // Force all packages to be re downloaded
yarn install --production // Install only production environment dependencies

[7] Release package

yarn publish

[8] Run script

yarn run // Used to execute in package Scripts defined under the scripts attribute in SHELLON

[9] Displays information about a package

yarn info [package] // It can be used to view the latest version information of a module

[10] Cache

yarn cache
yarn cache list // Lists each package that has been cached
yarn cache dir // Returns the global cache location
yarn cache clean // Clear cache

4, Comparison between yarn and npm commands

explainYarnNPM/CNPM
Initialize an itemyarn initnpm init
Default installation dependent packageyarn install/linknpm install/link
Install a dependency and save it to package by defaultyarn add taconpm install taco --save
Remove a dependencyyarn remove taconpm uninstall taco --save
Install a development time dependencyyarn add taco -devnpm install taco --save -dev
Update a dependent itemyarn upgrade taconpm update taco --save
Install a globally dependent projectyarn global add taconpm install taco --global
Publish / login / exit, a series of NPM operationsyarn publish/login/logoutnpm publish/login/logout
Run a commandyarn run/testnpm run/test

5, Supplement

npm install moduleName # Install the module into the project directory

npm install -g moduleName # -g means to install the module globally. The specific location of the module depends on the location of npm config prefix.

npm install -save moduleName # -save means to install the module in the project directory and write the dependencies in the dependencies node of the package file.

npm install -save-dev moduleName # -Save dev means to install the module in the project directory and write the dependencies in the devdependences node of the package file.

npm install moduleName command

  1. Install module to project node_modules directory.
  2. Module dependencies are not written to the devdependences or dependencies node.
  3. Modules are not downloaded when npm install is run to initialize the project.

npm install -g moduleName command

  1. Install the module to the global, not in the project node_ Save the module package in the modules directory.
  2. Module dependencies are not written to the devdependences or dependencies node.
  3. Modules are not downloaded when npm install is run to initialize the project.

npm install -save moduleName command

  1. Install module to project node_modules directory.
  2. Module dependencies are written to the dependencies node.
  3. When running npm install to initialize the project, the module will be downloaded to the project directory.
  4. Run npm install --production or specify node_ When the env variable value is production, the module will be automatically downloaded to node_modules directory.

NPM install - save dev modulename command

  1. Install module to project node_modules directory.
  2. Module dependencies are written to the devDependencies node.
  3. When running npm install to initialize the project, the module will be downloaded to the project directory.
  4. Run npm install --production or specify node_ When the env variable value is production, the module will not be automatically downloaded to node_modules directory.

summary

The modules under the devDependencies node are what we need to use during development, such as gulp used in the project, compressed css and shell modules. These modules are not required after the deployment of our project, so we can install them in the form of - save dev. Modules like express are necessary for project operation and should be installed under the dependencies node, so we should install them in the form of - save.

Keywords: node.js npm Yarn

Added by Nightslyr on Wed, 26 Jan 2022 01:46:33 +0200