Have you ever found yourself updating dependencies in a complex package.json, needing to determine which package versions are compatible with a specific peer dependency version? For example, let's say you're transitioning to Webpack 5 and must upgrade all outdated Webpack plugins. Typically, you would look at each plugin's documentation, hoping to find a peer version compatibility matrix. Another approach is to directly explore each plugin's registry entry, examining the peerDependencies across all published versions. This can be quite time-consuming for packages with extensive version histories, and I couldn't find an existing tool to automate this task. So, I wrote one:
const axios = require("axios");
const semver = require("semver");
async function minMaxSatisfy(packageName, peerPackage, version) {
try {
const response = await axios.get(
`https://registry.npmjs.org/${packageName}`,
);
const versions = response.data.versions;
const compatibleVersions = Object.keys(versions).filter((pkgVersion) => {
const peerDependencies = versions[pkgVersion].peerDependencies || {};
const peerDependencyRange = peerDependencies[peerPackage];
return (
peerDependencyRange && semver.satisfies(version, peerDependencyRange)
);
});
compatibleVersions.sort((a, b) => (semver.lt(a, b) ? -1 : 1));
return {
compatibleVersions,
min: compatibleVersions[0],
max: compatibleVersions[compatibleVersions.length - 1],
};
} catch (error) {
console.error(`Error fetching package info: ${error.message}`);
}
}
For example, to determine which versions of eslint-webpack-plugin
are
compatible with Webpack 5.89.0
, you can use the function like this:
minMaxSatisfy("eslint-webpack-plugin", "webpack", "5.89.0").then((res) =>
console.log(res),
);
This returns a list of compatible versions, including the earliest and latest compatible versions, in JSON format:
{
"compatibleVersions": [
"1.0.0",
"2.0.0",
"2.1.0",
"2.2.0",
"2.2.1",
"2.3.0",
"2.4.0",
"2.4.1",
"2.4.2",
"2.4.3",
"2.5.0",
"2.5.1",
"2.5.2",
"2.5.3",
"2.5.4",
"3.0.0",
"3.0.1",
"3.1.0",
"3.1.1",
"2.6.0",
"2.7.0",
"3.2.0",
"4.0.0",
"4.0.1"
],
"min": "1.0.0",
"max": "4.0.1"
}