Use of NPM to yarn

Time:2021-10-19

1、 Advantages of yarn and its differences from NPM

First, let’s talk about why a good NPM doesn’t need to use yarn. Of course, I haven’t realized its advantages yet, because I just used it, but as a rookie, I still want to use this better technology. Let’s talk about the advantages of yarn compared with NPM:

  1. Fast speed, mainly reflected in the following two aspects:

    • Parallel installation: both NPM and yarn perform a series of tasks during package installation. NPM executes each package according to the queue, that is, you must wait until the current package is installed before installing the next package. While yarn performs all tasks synchronously, which improves the performance.
    • Offline mode: if the software package has been installed before, it will be obtained from the cache when installing it again with yarn. There is no need to download it from the network like NPM
  2. Uniform installation version: in order to prevent pulling different versions, yarn has a lock file, yarn.lock, which records the version number of the module exactly installed. Each time a new module is added, yarn will create or update the yarn.lock file. This ensures that each time the same project dependency is pulled, a module version is used. In fact, NPM can use the same module version everywhere, but developers need to execute the NPM shrinkwrap command. This command will generate a lock file. When NPM install is executed, the lock file will be read first, which is the same as yarn reading yarn.lock file. The difference between NPM and yarn is that yarn will generate such a lock file by default, while NPM will generate the npm-shrinkwrap.json file through the shrinkwrap command. Only when this file exists, the version information of packages will be recorded and updated.
  3. More concise output: the output information of NPM is lengthy. When NPM install < package > is executed, all installed dependencies will be continuously printed on the command line. In contrast, yarn is too concise. By default, it combines Emoji to print out the necessary information intuitively and directly, and also provides some commands for developers to query additional installation information.
  4. Multi registration source processing: all dependent packages, no matter how many times they are indirectly associated by different libraries, will only be installed from one registration source, either NPM or bower, to prevent confusion and inconsistency.
  5. Better semantics: yarn has changed the names of some NPM commands, such as yarn add / remove, which is easier to understand and clearer than NPM’s original install / uninstall.
npm yarn
npm install yarn
npm install webpack –save yarn add webpack
npm uninstall webpack –save yarn remove webpack
npm install webpack –save-dev yarn add webpack -dev
npm update –save yarn upgrade
  • View version

    yarn –version
    npm -version (node -v)

  • Install Taobao image
yarn config set registry 'https://registry.npm.taobao.org'     
npm install -g cnpm --registry=http://registry.npm.taobao.org 
  • Initialize an item
    yarn init
    cnpm init
  • Default installation project dependency
    yarn
    npm install
  • Install a dependency and save it to package by default
    yarn add **
    npm install ** –save
  • Uninstall a project dependency
    yarn remove **
    cnpm uninstall ** –save
  • Update a project dependency
    yarn upgrade **
    cnpm update ** –save
  • Install a global project dependency
    yarn global add **
    cnpm install ** -g
  • Install project dependencies for a specific version number
    yarn add **@
    cnpm install **@1.1.1 –save
  • Publish, log in, log out, a series of NPM registry operations
    yarn publish/login/logout
    npm publish/login/logout
  • Run a command
    yarn run/test
    npm run/test

2、 How to convert from NPM to yarn environment

  1. If you haven’t used yarn, you should install yarn globally first
    npm install -g yarn
  2. Running yarn in a project where NPM already exists is equivalent to yarn install
    Yarn (after running successfully, the yarn.lock file will be generated in the project root directory)
  3. An error will be reported at this time. What node_ modules/webpack-dev-server/node_ Modules / ANSI regex / index.js can’t be found. I racked my brains to find out where the error was for a long time. Ah, later, I just ran it again. Fuck! If you are lucky, you may not make a mistake. Ha ha ha. Insert a little knowledge. This is because our domain name has a dead port number, so we must start with a fixed port number, otherwise we can’t open the page. So, the command to view the process under MAC: sudo lsof – I: 8086; Command to kill the process: sudo kill – 9 24830
  4. If you think yarn is not suitable for you, you can still go back to NPM without making any changes. If no one needs to use yarn for this project, you can delete your yarn. Lock document
  5. If your partner still uses NPM in the project and you use yarn, it’s good to submit both yarn.lock and package.lock.js directly.