How to update each dependency in package.json to the latest version?

前端 未结 30 2888
清歌不尽
清歌不尽 2020-11-22 08:01

I copied package.json from another project and now want to bump all of the dependencies to their latest versions since this is a fresh project and I don\'t mind

相关标签:
30条回答
  • 2020-11-22 08:10

    I recently had to update several projects that were using npm and package.json for their gruntfile.js magic. The following bash command (multiline command) worked well for me:

    npm outdated --json --depth=0 | \
    jq --ascii-output --monochrome-output '. | keys | .[]' | \
    xargs npm install $1 --save-dev
    

    The idea here: To pipe the npm outdated output as json, to jq
    (jq is a json command line parser/query tool)
    (notice the use of --depth argument for npm outdated)
    jq will strip the output down to just the top level package name only.
    finally xargs puts each LIBRARYNAME one at a time into a npm install LIBRARYNAME --save-dev command

    The above is what worked for me on a machine runnning: node=v0.11.10 osx=10.9.2 npm=1.3.24

    this required:
    xargs http://en.wikipedia.org/wiki/Xargs (native to my machine I believe)
    and
    jq http://stedolan.github.io/jq/ (I installed it with brew install jq)

    Note: I only save the updated libraries to package.json inside of the json key devDependancies by using --save-dev, that was a requirement of my projects, quite possible not yours.

    Afterward I check that everything is gravy with a simple

    npm outdated --depth=0
    

    Also, you can check the current toplevel installed library versions with

    npm list --depth=0
    
    0 讨论(0)
  • 2020-11-22 08:10

    Updtr!

    Based on npm outdated, updtr installs the latest version and runs npm test for each dependency. If the test succeeds, updtr saves the new version number to your package.json. If the test fails, however, updtr rolls back its changes.

    https://github.com/peerigon/updtr

    0 讨论(0)
  • 2020-11-22 08:11

    To update one dependency to its lastest version without having to manually open the package.json and change it, you can run

    npm install {package-name}@* {save flags?}
    

    i.e.

    npm install express@* --save
    

    For reference, npm-install


    Update: Recent versions may need latest flag instead, i.e. npm install express@latest


    As noted by user Vespakoen on a rejected edit, it's also possible to update multiple packages at once this way:

    npm install --save package-nave@* other-package@* whatever-thing@*
    

    He also apports a one-liner for the shell based on npm outdated. See the edit for code and explanation.


    PS: I also hate having to manually edit package.json for things like that ;)

    0 讨论(0)
  • 2020-11-22 08:11

    I use npm-check to achieve this.

    npm i -g npm npm-check
    npm-check -ug #to update globals
    npm-check -u #to update locals
    

    Another useful command list which will keep exact version numbers in package.json

    npm cache clean
    rm -rf node_modules/
    npm i -g npm npm-check-updates
    ncu -g #update globals
    ncu -ua #update locals
    npm i
    
    0 讨论(0)
  • 2020-11-22 08:13

    As of npm version 5.2.0, there is a way to run this in a single line without installing any additional packages to your global npm registry nor locally to your application. This can be done by leveraging the new npx utility that's bundled with npm. (Click here to learn more.)

    Run the following command in the root of your project:

    npx npm-check-updates -u && npm i
    
    0 讨论(0)
  • 2020-11-22 08:17

    To see which packages have newer versions available, then use the following command:

    npm outdated
    

    to update just one dependency just use the following command:

    npm install yourPackage@latest --save
    

    For example:

    My package.json file has dependency:

    "@progress/kendo-angular-dateinputs": "^1.3.1",
    

    then I should write:

    npm install @progress/kendo-angular-dateinputs@latest --save
    
    0 讨论(0)
提交回复
热议问题