The EINVALIDSEMVER error occurs when npm encounters a version string that doesn't follow semantic versioning rules. This can be in package.json or when specifying versions to install.
Fixes npm ERR! code EINVALIDSEMVER
1.0.0 # Basic version
1.0.0-alpha # Prerelease
1.0.0-beta.1 # Numbered prerelease
1.0.0-rc.1 # Release candidate
1.0.0+build.123 # Build metadata1.0.0 # Basic version1.0.0-alpha # Prerelease1.0.0-beta.1 # Numbered prerelease1.0.0-rc.1 # Release candidate1.0.0+build.123 # Build metadatanpm ERR! code EINVALIDSEMVER
Semantic versioning (semver) is a strict format: MAJOR.MINOR.PATCH with optional prerelease and build metadata. npm uses this format to determine version compatibility, ordering, and dependency resolution. This error occurs when a version string doesn't match the expected format, making it impossible for npm to compare versions or satisfy dependency requirements.
Valid semver formats:
1.0.0 # Basic version
1.0.0-alpha # Prerelease
1.0.0-beta.1 # Numbered prerelease
1.0.0-rc.1 # Release candidate
1.0.0+build.123 # Build metadataCommon invalid versions:
// WRONG
"1.0" // Missing patch
"v1.0.0" // No "v" prefix
"1.0.0.0" // Too many parts
"01.0.0" // Leading zero
// CORRECT
"1.0.0"Valid range formats:
"dependencies": {
"pkg": "^1.0.0", // Compatible
"pkg": "~1.0.0", // Approximately
"pkg": ">=1.0.0", // Greater/equal
"pkg": "1.0.0 - 2.0.0", // Range
"pkg": "1.x || 2.x" // Or
}Let npm manage versions:
# Set valid version
npm version 1.0.0
# Increment properly
npm version patch # 1.0.0 -> 1.0.1
npm version minor # 1.0.1 -> 1.1.0
npm version major # 1.1.0 -> 2.0.0Check if version is valid:
# Using semver package
npx semver 1.0.0 # Valid - returns 1.0.0
npx semver v1.0 # Invalid - returns nothing
# Check range
npx semver -r "^1.0.0" 1.0.5 # Returns 1.0.5 if in rangeCheck and fix dependencies:
# Show dependency versions
npm ls
# Update to valid versions
npm update
# Check for outdated
npm outdatednpm's semver implementation follows the semver.org specification. Use semver.org's calculator to understand version matching. For Git-based dependencies, ensure tags follow semver format without "v" prefix. Some legacy packages may have non-semver versions - pin to specific versions if needed. The semver npm package can help validate and compare versions programmatically.