One line command to update all npm dependent packages

npm packages are updated very quickly in order to update projects or global dependencies to the latest version. The traditional method is to update one by one, such as updating react to the latest version. The command is as follows:

# npm
npm i --save react@latest
# yarn
yarn add react@latest
 Copy code

yarn is a new generation of js package manager invented by facebook, which supports offline use. This is a comparison between npm and yarn's commands.

However, this practice is quite time-consuming. Is there a simpler way? The answer is to use npm-check perhaps yarn . Both require a global installation.

npm i -g yarn
npm i -g npm-check
 Copy code

Update project dependencies using NPM check

Run at project root

npm-check -u
 Copy code

The output is as follows:

? Choose which packages to update. (Press <space> to select)

 Update package.json to match version installed.
❯◯ chalk     ^1.1.3   ❯  2.4.2   https://github.com/chalk/chalk#readme
 ◯ cheerio   ^0.22.0  ❯  0.22.0  https://github.com/cheeriojs/cheerio#readme
 ◯ debug     ^2.3.3   ❯  4.1.1   https://github.com/visionmedia/debug#readme
 ◯ log4js    ^1.0.1   ❯  4.1.0   https://log4js-node.github.io/log4js-node/
 ◯ mustache  ^2.3.0   ❯  3.0.1   https://github.com/janl/mustache.js
 ◯ request   2.79.0   ❯  2.88.0  https://github.com/request/request#readme
 ◯ unescape  ^0.2.0   ❯  1.0.1   https://github.com/jonschlinkert/unescape
 ◯ yargs     ^6.4.0   ❯  13.2.2  https://yargs.js.org/

 Space to select. Enter to start upgrading. Control-C to cancel.
Copy code

Space to switch whether the package is updated. Control + C cancels the update. Press enter to execute the update.

Updating project dependencies with yarn

Run at project root

yarn upgrade-interactive  --latest
 Copy code

The output is as follows:

yarn upgrade-interactive v1.15.2
info Color legend :
 "<red>"    : Major Update backward-incompatible updates
 "<yellow>" : Minor Update backward-compatible features
 "<green>"  : Patch Update backward-compatible bug fixes
? Choose which packages to update. (Press <space> to select, <a> to toggle all,
<i> to invert selection)
 dependencies
   name      range   from       to          url
❯◯ chalk     latest  1.1.3   ❯  2.4.2       https://github.com/chalk/chalk#readm
e
 ◯ cheerio   latest  0.22.0  ❯  1.0.0-rc.3  https://github.com/cheeriojs/cheerio
#readme
 ◯ debug     latest  2.6.9   ❯  4.1.1       https://github.com/visionmedia/debug
#readme
 ◯ log4js    latest  1.1.1   ❯  4.1.0       https://log4js-node.github.io/log4js
-node/
 ◯ mustache  latest  2.3.2   ❯  3.0.1       https://github.com/janl/mustache.js
 ◯ request   latest  2.79.0  ❯  2.88.0      https://github.com/request/request#r
eadme
 ◯ unescape  latest  0.2.0   ❯  1.0.1       https://github.com/jonschlinkert/une
scape
 ◯ yargs     latest  6.6.0   ❯  13.2.2      https://yargs.js.org/
Copy code

yarn provides the function of selecting all, that is, press A to switch whether the package is updated, Control + C to cancel the update, and press enter to execute the update.

The yarn update command is too long. Who can remember? In this case, please use the help of command-line tools reasonably, such as running yarn help.

Update command cross reference table

Update global dependency as above

explainyarnnpm-check
Update project dependencies without interactionyarn upgrade --latestnpm-check -y
Update project dependencies, interactiveyarn upgrade-interactive --latestnpm-check -u
Update global dependencies without interactionyarn global upgrade --latestnpm-check -g -y
Update global dependencies, interactiveyarn global upgrade-interactive --latestnpm-check -g -u

Detection principle

Yarn checks whether the version is the latest according to the yarn.lock file, so the project uses npm to install the dependent package. Run yarn install before updating.

NPM check is to detect the package.json file. Node exists in the project_ The modules folder can be updated.

Update reminder

No interaction means that the dependent package is directly updated to the latest version. Interactive update is recommended, and there will be an update warning message.

The API may change significantly with the latest dependent packages. In order to update smoothly, please git commit before updating. If the update fails, it can be returned smoothly.

cnpm is not recommended

In order to speed up the installation of the installation dependency, the colleague Amway cnpm may be used, but this will cause the dependent installation of the package to be abnormal and the project cannot run.

A better approach is to use nrm Switch the download source.

Usually, you use yarn to pack and npm to run scripts.

Install nrm

npm i -g nrm 
Copy code

View and download image source

nrm ls
 Copy code

The output is as follows

  npm ---- https://registry.npmjs.org/
  cnpm --- http://r.cnpmjs.org/
* taobao - https://registry.npm.taobao.org/
  nj ----- https://registry.nodejitsu.com/
  npmMirror  https://skimdb.npmjs.com/registry/
  edunpm - http://registry.enpmjs.org/
Copy code

Switch mirror source

nrm use taobao
 Copy code

The package command remains unchanged, such as installing react.

# npm
npm i --save react
# yarn
yarn add react
 Copy code

Experience the general packing speed of flying. It is no longer packing for one hour and five minutes.


Author: nusr
Link: https://juejin.cn/post/6844903827599015944
Source: Nuggets
The copyright belongs to the author. For commercial reprint, please contact the author for authorization, and for non-commercial reprint, please indicate the source.

Keywords: node.js React

Added by Lerris on Mon, 27 Sep 2021 03:53:37 +0300