This command sequence updates all outdated npm packages in a project to their latest versions using pnpm. Below is a breakdown of each part of the command:
npm outdated | cut -d" " -f1 | tail -n +2 | sed 's/$/@latest/' | xargs pnpm i
- Lists all the dependencies that are outdated in your project.
- Outputs details such as the current version, wanted version, and latest version available for each package.
cut
is used to extract specific fields from each line of input.-d" "
sets the delimiter to a space character.-f1
extracts the first field, which is the package name.
tail
outputs the last part of files.-n +2
tellstail
to start from the second line, skipping the header line fromnpm outdated
.
sed
is a stream editor for parsing and transforming text.s/$/@latest/
substitutes the end of each line ($
) with@latest
, appending@latest
to each package name.
xargs
builds and executes command lines from standard input.pnpm i
(orpnpm install
) is the pnpm command to install packages.xargs
takes the list of packages generated by the previous commands and runspnpm i
for each one, installing the latest version of each outdated package.
Consider a project with the following outdated packages:
$ npm outdated
Package Current Wanted Latest Location
lodash 4.17.15 4.17.21 4.17.21 myproject
express 4.17.1 4.17.1 4.18.2 myproject
Running the command:
npm outdated | cut -d" " -f1 | tail -n +2 | sed 's/$/@latest/' | xargs pnpm i
Will install the latest versions of lodash and express:
pnpm i lodash@latest express@latest
This updates lodash to 4.17.21 and express to 4.18.2.
This command sequence is a convenient way to update all outdated packages in your project to their latest versions.