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

前端 未结 30 2906
清歌不尽
清歌不尽 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:30

    Updated for latest NPM

    npm 2+ (Node 0.12+):

    
    npm outdated
    npm update
    git commit package-lock.json
    

    Ancient npm (circa 2014):

    npm install -g npm-check-updates
    npm-check-updates
    npm shrinkwrap
    git commit package-lock.json
    

    Be sure to shrinkwrap your deps, or you may wind up with a dead project. I pulled out a project the other day and it wouldn't run because my deps were all out of date/updated/a mess. If I'd shrinkwrapped, npm would have installed exactly what I needed.


    Details

    For the curious who make it this far, here is what I recommend:

    Use npm-check-updates or npm outdated to suggest the latest versions.

    # `outdated` is part of newer npm versions (2+)
    $ npm outdated
    # If you agree, update.  
    $ npm update
    
    #       OR
    
    # Install and use the `npm-check-updates` package.
    $ npm install -g npm-check-updates
    # Then check your project
    $ npm-check-updates
    # If you agree, update package.json.
    $ npm-check-updates -u
    

    Then do a clean install (w/o the rm I got some dependency warnings)

    $ rm -rf node_modules
    $ npm install 
    

    Lastly, save exact versions to npm-shrinkwrap.json with npm shrinkwrap

    $ rm npm-shrinkwrap.json
    $ npm shrinkwrap
    

    Now, npm install will now use exact versions in npm-shrinkwrap.json

    If you check npm-shrinkwrap.json into git, all installs will use the exact same versions.

    This is a way to transition out of development (all updates, all the time) to production (nobody touch nothing).

    • npm outdated

    • npm-check-updates

    • npm shrinkwrap

    p.s. Yarn is sending your package list to Facebook.

提交回复
热议问题