yarnpkg/yarn · error · MessageError
invalidVersion
Error message
invalidVersion
What it means
Thrown by 'yarn version' when --new-version is provided but isValidNewVersion(oldVersion, newVersion, config.looseSemver, identifier) returns false. The proposed version is not a valid semver relative to the existing version (and optional preid).
Source
Thrown at src/cli/commands/version.js:89
return flags.commitHooks === false || config.getOption('version-commit-hooks') === false;
}
if (pkg.scripts) {
// inherit `scripts` from manifest
Object.assign(scripts, pkg.scripts);
}
// get old version
let oldVersion = pkg.version;
if (oldVersion) {
reporter.info(`${reporter.lang('currentVersion')}: ${oldVersion}`);
} else {
oldVersion = '0.0.0';
}
// get new version
if (newVersion && !isValidNewVersion(oldVersion, newVersion, config.looseSemver, identifier)) {
throw new MessageError(reporter.lang('invalidVersion'));
}
// get new version by bumping old version, if requested
if (!newVersion) {
if (flags.major) {
newVersion = semver.inc(oldVersion, 'major');
} else if (flags.minor) {
newVersion = semver.inc(oldVersion, 'minor');
} else if (flags.patch) {
newVersion = semver.inc(oldVersion, 'patch');
} else if (flags.premajor) {
newVersion = semver.inc(oldVersion, 'premajor', identifier);
} else if (flags.preminor) {
newVersion = semver.inc(oldVersion, 'preminor', identifier);
} else if (flags.prepatch) {
newVersion = semver.inc(oldVersion, 'prepatch', identifier);
} else if (flags.prerelease) {
newVersion = semver.inc(oldVersion, 'prerelease', identifier);View on GitHub (pinned to c2dda503f3)
Solutions
- Provide a concrete, valid semver: 'yarn version --new-version 1.2.3'.
- If you want to bump rather than set an exact version, use --patch/--minor/--major instead.
- When using --preid, ensure --new-version is a valid prerelease form (e.g. '1.2.3-beta.1') consistent with the identifier.
Example fix
# before $ yarn version --new-version 1.x # after $ yarn version --new-version 1.2.3
Defensive patterns
Strategy: validation
Validate before calling
import semver from 'semver';
function assertValidNewVersion(oldVersion: string, newVersion: string, loose: boolean, identifier?: string) {
if (!isValidNewVersion(oldVersion, newVersion, loose, identifier)) {
throw new Error(`Invalid version "${newVersion}" relative to current "${oldVersion}"`);
}
}
// or simply: if (!semver.valid(newVersion)) throw ... Type guard
import semver from 'semver';
function isValidVersionInput(v: unknown): v is string {
return typeof v === 'string' && semver.valid(v) !== null;
} Prevention
- Validate candidate versions with semver.valid before passing them to 'yarn version'.
- Prefer bump flags to avoid hand-typed version strings.
- Keep --preid and --new-version consistent in release scripts.
When it happens
Trigger: Running 'yarn version --new-version <bad>' where <bad> is e.g. 'foo', '1.x', '1.2', or otherwise fails semver validation given the current version and identifier.
Common situations: Typo in the version string; user passed a range instead of a concrete version; pre-release identifier mismatch with --preid; loose-semver disabled and the value is too permissive.
Related errors
AI-assisted analysis of yarnpkg/yarn@c2dda503f3 (2026-08-13).
Data as JSON: /api/errors/9195c019929cb66e.
Report an issue: GitHub.