diff --git a/.changeset/olive-fans-beg.md b/.changeset/olive-fans-beg.md new file mode 100644 index 000000000000..915c5a375a38 --- /dev/null +++ b/.changeset/olive-fans-beg.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Updates `astro add` to handle installing peer dependencies with optional npm specifiers diff --git a/packages/astro/src/cli/add/index.ts b/packages/astro/src/cli/add/index.ts index e452f08d688d..7f0263bcb2e9 100644 --- a/packages/astro/src/cli/add/index.ts +++ b/packages/astro/src/cli/add/index.ts @@ -693,7 +693,16 @@ async function resolveRangeToInstallSpecifier(name: string, range: string): Prom if (versions instanceof Error) return name; // Filter out any prerelease versions const stableVersions = versions.filter((v) => !v.includes('-')); + // Peer dependencies may sometimes use the `npm:` specifier, but `semver` package panics with it. + // Filter it out if so. + if (range.includes('npm:')) { + range = range + .split('||') + .filter((r) => !r.includes('npm:')) + .join('||'); + } const maxStable = maxSatisfying(stableVersions, range); + if (maxStable === null) return name; return `${name}@^${maxStable}`; }