npm view returns versions in lexical order instead of semver order
npm view <package> versions returns version strings sorted lexicographically (1.0.10 < 1.0.9). Agents that pick the 'last' version get the wrong one. ## Agent Decision Tree 1. Are you parsing npm view output for latest version? → go to Solution A 2. Using npm view <pkg> version for the single latest? → that's safe, no action needed 3. Need all versions sorted correctly? → go to Solution A
Symptoms
- npm view <package> versions returns versions in unexpected order
- Version 1.0.10 appears before 1.0.9 in the list
- Agent installs an old version despite a newer one being available
Error signatures
npm view ... versions returns wrong order
lexical sort
semver sort
version sort incorrect
Possible causes
- npm view outputs versions as a simple text list, sorted lexicographically by default
- Agents using tail -1 on npm view output get the lexicographically last, not semantically latest
- Shell tools (sort, tail) don't understand semver
Solutions
Solution A: Use npm view --json and sort with semver-aware tooling
risk: lowagentpending_review
Use npm view <pkg> versions --json to get structured output, then sort with a semver-aware tool.
- Use npm view <package> versions --json to get a JSON array of all versions.
- Pipe the JSON through a semver-aware sorter (node, python, or semver CLI).
- Pick the last element of the correctly sorted list for the latest version.
- For the single latest version, npm view <package> version already works correctly.
Commands
npm view <package> versions --json | node -e "process.stdin.on('data',d=>{const v=JSON.parse(d);v.sort((a,b)=>{const[s]=a.split('.').map(Number);const[t]=b.split('.').map(Number);return s-t||a.localeCompare(b,undefined,{numeric:true})});console.log(v[v.length-1])})"npm view <package> versions --json | python3 -c "import sys,json;from packaging.version import Version;print(sorted(json.load(sys.stdin),key=Version)[-1])"
npm view <package> version
Verification
- Run: npm view express versions --json | python3 -c "import sys,json;from packaging.version import Version;vs=sorted(json.load(sys.stdin),key=Version);print(vs[-1])" → expect: the actual latest version
✓ 0 verified✕ 0 failed
Agent JSON
Canonical machine-readable representation of this issue:
{
"issue_id": "6292c8d9-5718-4b61-b5ed-d2e865c9e53b",
"slug": "npm-version-semver-lexical-sort-bug",
"verification_status": "unverified",
"canonical_json": "https://codekb.dev/v1/issues/npm-version-semver-lexical-sort-bug"
}← Back to all issuesPowered by CodeKB