yarnpkg/yarn · error · MessageError
requiredVersionInRange
Error message
requiredVersionInRange
What it means
Thrown in the 'tag add' subcommand when normalizePattern(args.shift()) yields hasVersion === false. The first argument to 'tag add' must be a 'name@version' pattern; without a version component, there is nothing to point the tag at.
Source
Thrown at src/cli/commands/tag.js:95
return true;
}
}
export function setFlags(commander: Object) {
commander.description('Add, remove, or list tags on a package.');
}
export const {run, hasWrapper, examples} = buildSubCommands(
'tag',
{
async add(config: Config, reporter: Reporter, flags: Object, args: Array<string>): Promise<boolean> {
if (args.length !== 2) {
return false;
}
const {name, range, hasVersion} = normalizePattern(args.shift());
if (!hasVersion) {
throw new MessageError(reporter.lang('requiredVersionInRange'));
}
if (!isValidPackageName(name)) {
throw new MessageError(reporter.lang('invalidPackageName'));
}
const tag = args.shift();
reporter.step(1, 3, reporter.lang('loggingIn'));
const revoke = await getToken(config, reporter, name);
reporter.step(2, 3, reporter.lang('creatingTag', tag, range));
const result = await config.registries.npm.request(
`-/package/${NpmRegistry.escapeName(name)}/dist-tags/${encodeURI(tag)}`,
{
method: 'PUT',
body: range,
},
);View on GitHub (pinned to c2dda503f3)
Solutions
- Provide the version inline: 'yarn tag add <name>@<version> <tag>'.
- Verify the version exists on the registry before tagging it.
- Use a concrete version (e.g. 1.2.3) or a resolvable range that resolves to a single published version.
Example fix
# before $ yarn tag add my-pkg latest # after $ yarn tag add my-pkg@1.2.3 latest
Defensive patterns
Strategy: validation
Validate before calling
const {name, range, hasVersion} = normalizePattern(input);
if (!hasVersion) {
throw new Error(`Pattern "${input}" must include a version, e.g. "${input}@1.2.3"`);
} Type guard
function patternHasVersion(input: string): boolean {
// name@version: the last '@' must not be at index 0 (scope) and must be followed by a non-empty range
const at = input.lastIndexOf('@');
return at > 0 && at < input.length - 1;
} Prevention
- Always pass 'name@version' to 'yarn tag add'.
- Resolve the version programmatically (e.g. from semver.highest on the registry) before constructing the arg.
- Document the expected CLI form in project scripts.
When it happens
Trigger: Running 'yarn tag add <name> <tag>' where <name> has no '@<version>' suffix, so normalizePattern reports no version segment.
Common situations: User assumes 'tag add' takes name and version as separate args; forgot the '@'; pattern uses ':' or '=' instead of '@'.
Related errors
AI-assisted analysis of yarnpkg/yarn@c2dda503f3 (2026-08-13).
Data as JSON: /api/errors/311fa05dcbf242f2.
Report an issue: GitHub.