npm-check-updates使用手順


npm update,只能按照package.json中標注的版本號進行更新,升級后不會修改package.json中的版本號,需要自己手動修改,比較麻煩。
npm-check-updates 升級插件升級后會自動修改package.json里的版本號,簡單方便。

安裝

npm install -g npm-check-updates

使用

1.輸入ncu命令檢查包最新版本

$ ncu
Checking package.json
[====================] 5/5 100%
 
 express           4.12.x  →   4.13.x
 multer            ^0.1.8  →   ^1.0.1
 react-bootstrap  ^0.22.6  →  ^0.24.0
 react-a11y        ^0.1.1  →   ^0.2.6
 webpack          ~1.9.10  →  ~1.10.5
 
Run ncu -u to upgrade package.json

2.根據提示輸入ncu -u更新package.json,然后根據提示輸入npm install安裝新版本即可

$ ncu -u
Upgrading package.json
[====================] 1/1 100%
 
 express           4.12.x  →   4.13.x
 
Run npm install to install new versions.

3.更新全局包

$ ncu -g

4.--filter 和 --reject選項,分別可用來包含和忽略指定包

# match mocha and should packages exactly 
$ ncu mocha             # shorthand for ncu -f mocha (or --filter) 
$ ncu one, two, three
 
# exclude packages 
$ ncu -x nodemon        # shorthand for ncu --reject nodemon 
 
# match packages that start with "gulp-" using regex 
$ ncu "/^gulp-.*$/"
 
# match packages that do not start with "gulp-". 
$ ncu '/^(?!gulp-).*$/' # mac/linux 
$ ncu "/^(?!gulp-).*$/" # windows

其他選項:

--configFilePath         rc config file path (default: directory of `packageFile` or ./ otherwise)
--configFileName         rc config file name (default: .ncurc.{json,yml,js})                             
--cwd                    Used as current working directory for `spawn` in npm listing
--dep                    check only a specific section(s) of dependencies:
                         prod|dev|peer|optional|bundle (comma-delimited)
--engines-node           include only packages that satisfy engines.node as
                         specified in the package file
-e, --error-level        set the error-level. 1: exits with error code 0 if no
                         errors occur. 2: exits with error code 0 if no
                         packages need updating (useful for continuous
                         integration)
-f, --filter             include only package names matching the given string,
                         comma-or-space-delimited list, or /regex/
-g, --global             check global packages instead of in the current project
-i, --interactive        Enable interactive prompts for each dependency;
                         Implies -u unless one of the json options are set
-j, --jsonAll            output new package file instead of human-readable
                         message
--jsonDeps               Will return output like `jsonAll` but only lists
                         `dependencies`, `devDependencies`, and
                         `optionalDependencies` of the new package data.
--jsonUpgraded           output upgraded dependencies in json
-l, --loglevel           what level of logs to report: silent, error, warn,
                         info, verbose, silly (default: warn)
-m, --minimal            do not upgrade to newer versions that are already
                         satisfied by the existing version range (v2 behavior).
-n, --newest             find the newest published versions available instead
                         of the latest stable versions
-p, --packageManager     npm or bower (default: npm)
--packageData            include stringified package file (use stdin instead)
--packageFile            package file location (default: ./package.json)
--pre                    include -alpha, -beta, -rc. Default: 0. Default
                         with --newest and --greatest: 1.
--prefix                 Used as current working directory in bower and npm
-r, --registry           specify third-party NPM registry
--removeRange            remove version ranges from the final package version
-s, --silent             don't output anything (--loglevel silent)
--semverLevel            find the highest version within "major" or "minor"
-t, --greatest           find the highest versions available instead of the
                         latest stable versions
--timeout                a global timeout in ms
-u, --upgrade            overwrite package file
-v, --version            get version
-V                       get version
-x, --reject             exclude packages matching the given string, comma-
                         delimited list, or regex

5.配置文件.ncurc.{json,yml,js},在命令行中可使用--configFileName、 --configFilePath指定配置文件

.ncurc.json:

{
  "upgrade": true,
  "filter": "express",
  "reject": [
    "@types/estree",
    "ts-node"
  ]
}

6.模塊中使用

const ncu = require('npm-check-updates');
 
ncu.run({
    // Any command-line option can be specified here.
    // These are set by default:
    jsonUpgraded: true,
    packageManager: 'npm',
    silent: true
}).then((upgraded) => {
    console.log('dependencies to upgrade:', upgraded);
});

詳情參照:https://www.npmjs.com/package/npm-check-updates


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM