Search code examples
node.jsnpmyarnpkg

Using yarn, how to update package.json with actual version installed of packages?


I'm working on a project where dependencies version are not strictly fixed (using the ^), like so:

...
"dependencies": {
    "react": "^16.13.1",
    "react-dom": "^16.13.1"
}
...

Now, I'd like to strictly fix all version (remove the ^) but I'd like to keep the version actually installed (and saved in yarn.lock) at the moment and write it to package.json.

For example, for react: ^16.13.1, installed version is 16.14.0. So I'd like my package.json to be:

...
"dependencies": {
    "react": "16.14.0",
    "react-dom": "16.14.0"
}
...

Is there a command to do this automatically?


Solution

  • The solution came from npm-check-updates.

    Running this command, it updates my package.json with latest minor version for all packages:

    ncu -t minor -u  
    

    Then, a little yarn install to actually install these packages and you can safely remove all the ^ of your package.json.