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

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

    I really like how npm-upgrade works. It is a simple command line utility that goes through all of your dependencies and lets you see the current version compared to the latest version and update if you want.

    Here is a screenshot of what happens after running npm-upgrade in the root of your project (next to the package.json file):

    For each dependency you can choose to upgrade, ignore, view the changelog, or finish the process. It has worked great for me so far.

    EDIT: To be clear this is a third party package that needs to be installed before the command will work. It does not come with npm itself:

    npm install -g npm-upgrade
    

    Then from the root of a project that has a package.json file:

    npm-upgrade
    
    0 讨论(0)
  • 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.

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

    If you are using yarn, yarn upgrade-interactive is a really sleek tool that can allow you to view your outdated dependencies and then select which ones you want to update.

    More reasons to use Yarn over npm. Heh.

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

    This works as of npm 1.3.15.

    "dependencies": {
      "foo": "latest"
    }
    
    0 讨论(0)
  • 2020-11-22 08:32

    This feature has been introduced in npm v5. update to npm using npm install -g npm@latest and

    to update package.json

    1. delete /node_modules and package-lock.json (if you have any)

    2. run npm update. this will update the dependencies package.json to the latest, based on semver.

    to update to very latest version. you can go with npm-check-updates

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

    Try following command if you using npm 5 and node 8

    npm update --save

    0 讨论(0)
提交回复
热议问题