Mastering 'pnpm outdated' Command (with examples)
The pnpm outdated
command is a powerful tool for developers working with Node.js projects. It helps in tracking the latest versions of packages against the currently installed ones in your project. This capability is crucial for maintaining your project dependencies up to date, ensuring that you benefit from the latest features, security patches, and bug fixes. This article explores various use cases of the pnpm outdated
command, demonstrating its flexibility and utility in managing dependencies effectively.
Use case 1: Check for outdated packages
Code:
pnpm outdated
Motivation:
Regularly checking for outdated packages in your project is essential for two primary reasons: security and performance. By running this command, developers can quickly identify which packages have newer versions available, ensuring their applications are secure and optimized with the latest updates.
Explanation:
pnpm
: This part of the command refers to the package manager itself, which is responsible for handling dependencies in Node.js projects.outdated
: This command checks the versions of installed packages against the latest versions available in the registry.
Example Output:
Package Current Wanted Latest
express 4.17.1 4.17.1 5.0.0
lodash 4.17.20 4.17.21 4.17.21
Use case 2: Check for outdated dependencies found in every workspace package
Code:
pnpm outdated -r
Motivation:
In projects utilizing a monorepo structure with multiple sub-packages, ensuring that each package is using up-to-date dependencies is crucial. The recursive check across all workspace packages is an efficient way to guarantee consistency.
Explanation:
-r
: This option stands for “recursive,” meaning the command will traverse through all packages in the workspace and provide outdated dependency information for each.
Example Output:
mypackage
Package Current Wanted Latest
express 4.17.1 4.17.1 5.0.0
anotherpackage
Package Current Wanted Latest
react 16.13.1 16.14.0 17.0.0
Use case 3: Filter outdated packages using a package selector
Code:
pnpm outdated --filter package_selector
Motivation:
Filtering is useful when a project has a large number of packages, and the focus needs to be on a specific subset. This reduces noise and helps in honing in on just the packages relevant for current work.
Explanation:
--filter
: This option allows developers to specify a selector (pattern) to limit the outdated check to specific packages.
Example Output:
Matching packages:
lodash
Package Current Wanted Latest
lodash 4.17.20 4.17.21 4.17.21
Use case 4: List outdated packages globally
Code:
pnpm outdated --global
Motivation:
Global dependencies affect all Node.js projects on a machine. Keeping these updated is important for ensuring that tools and packages used across multiple projects are current and functional.
Explanation:
--global
: This flag instructspnpm
to check the outdated status of globally installed packages, rather than project-specific ones.
Example Output:
Global Packages
Package Current Wanted Latest
npm 6.14.11 6.14.12 7.0.0
Use case 5: Print details of outdated packages
Code:
pnpm outdated --long
Motivation:
When a simple list of outdated packages isn’t enough, having detailed information about why a package should be updated, or additional metadata, can inform decision-making about whether to update.
Explanation:
--long
: This option increases the verbosity of the output, showing additional details about each package, such as the types of changes in the newer versions.
Example Output:
Package Current Wanted Latest Details
express 4.17.1 4.17.1 5.0.0 Major release with new features
Use case 6: Print outdated dependencies in a specific format
Code:
pnpm outdated --format json
Motivation:
Incorporating pnpm outdated
into scripts or tools sometimes requires the output to be in a machine-readable format, such as JSON, to easily parse and manipulate the data programmatically.
Explanation:
--format format
: Specifies the output format. Usingjson
results in a JSON structure, suitable for further processing.
Example Output:
{
"express": {
"current": "4.17.1",
"wanted": "4.17.1",
"latest": "5.0.0"
},
"lodash": {
"current": "4.17.20",
"wanted": "4.17.21",
"latest": "4.17.21"
}
}
Use case 7: Print only versions that satisfy specifications in package.json
Code:
pnpm outdated --compatible
Motivation:
This is particularly helpful when managing projects where exact version constraints are defined and only those which are strictly within those constraints are updated, avoiding unexpected breaking changes.
Explanation:
--compatible
: Restricts the reported outdated packages to those that still meet any version constraints specified inpackage.json
.
Example Output:
Package Current Compatible Latest
lodash 4.17.20 4.17.21 4.17.21
Use case 8: Check only outdated Dev dependencies
Code:
pnpm outdated --dev
Motivation:
Development dependencies (e.g., testing tools) can bloat a project if not maintained properly. By focusing on dev dependencies, you can make sure that your development environment stays robust and efficient without unnecessary bulk.
Explanation:
--dev
: This option limits the outdated check to development dependencies only, ignoring production dependencies.
Example Output:
Dev Dependencies
Package Current Wanted Latest
mocha 7.1.1 8.0.0 9.0.0
Conclusion:
The pnpm outdated
command is an essential tool for maintaining the health of a Node.js project’s dependencies. By understanding and utilizing its various options and filters, developers can ensure that all dependencies, whether local, global, production, or development-specific, remain current. This practice not only enhances security but also optimizes the functionality and performance of applications.