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
explain | Yarn | NPM/CNPM |
---|---|---|
Initialize an item | yarn init | npm init |
Default installation dependent package | yarn install/link | npm install/link |
Install a dependency and save it to package by default | yarn add taco | npm install taco --save |
Remove a dependency | yarn remove taco | npm uninstall taco --save |
Install a development time dependency | yarn add taco -dev | npm install taco --save -dev |
Update a dependent item | yarn upgrade taco | npm update taco --save |
Install a globally dependent project | yarn global add taco | npm install taco --global |
Publish / login / exit, a series of NPM operations | yarn publish/login/logout | npm publish/login/logout |
Run a command | yarn run/test | npm 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
- Install module to project node_modules directory.
- Module dependencies are not written to the devdependences or dependencies node.
- Modules are not downloaded when npm install is run to initialize the project.
npm install -g moduleName command
- Install the module to the global, not in the project node_ Save the module package in the modules directory.
- Module dependencies are not written to the devdependences or dependencies node.
- Modules are not downloaded when npm install is run to initialize the project.
npm install -save moduleName command
- Install module to project node_modules directory.
- Module dependencies are written to the dependencies node.
- When running npm install to initialize the project, the module will be downloaded to the project directory.
- 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
- Install module to project node_modules directory.
- Module dependencies are written to the devDependencies node.
- When running npm install to initialize the project, the module will be downloaded to the project directory.
- 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.