DEV Community

loading...

Update to the latest version of many npm dependencies at once

camillehdl profile image Camille Hodoul Originally published at camillehdl.dev ・1 min read

I couldn't find a npm command to update a subset of my dependencies to their latest version based on a name pattern, so here's a one-liner to do it with pipes and awk (to be modified for your needs).

In this example, I want to update all the dependencies containing the string "babel".

npm outdated |awk 'BEGIN{OFS="@"} $1 ~ /babel/ { print $1, "latest" }'| xargs npm install
Enter fullscreen mode Exit fullscreen mode

Explanation of each command

npm outdated lists your outdated dependencies.

awk:

  • BEGIN{OFS="@"} sets @ as the output field separator (will be used by print)
  • $1 ~ /babel/ will match the lines containing "babel" in their first column
  • { print $1, "latest" } will output each selected lines concatenated with "latest" (using "@" as the OFS)

xargs npm install will give the output of awk as input arguments to npm install, like so : npm install dependency1@latest dependency2@latest ...

Tweak it

The beauty of the command line: you could tweak this for different dependency managers, such as Composer for PHP.

Discussion (0)

Forem Open with the Forem app