About This Article
This article was created using an automated generation workflow utilizing generative AI. We reviewed the official npm/node-semver documentation and official npm documentation, and executed SemVer range evaluations in Node.js for verification.Verification Status: ✅ Execution verified in Node.js – Pages and WordPress display not verified
package.json frequently seen in ^1.2.3 is interpreted in semver as >=1.2.3 <2.0.0-0. If the beginning is 0, the upper bound changes, and ^0.2.3 is >=0.2.3 <0.3.0-0、^0.0.3 is >=0.0.3 <0.0.4-0. The upper bound -0 is a comparison value used to exclude boundary prereleases from the upper limit. In this article, we use a live demo and Node.js to verify which candidates fall within the range.
First, Compare with the Live Demo
Open the semver live demoand enter ^0.2.3 in the range field to execute. In the results, 0.2.9 will be "in range" and 0.3.0 will be "out of range". If you then try ^1.2.3、^0.0.3、~1.2.3、1.3.0-beta.1, you can observe the difference between the upper bound and prereleases.
This demo providesimplementation HTML,、tutorial README,、Node.js code,、and 5 test items.The core semver 7.8.5 is included in the repositorybundledalong with theISC license. No external communication is required for the semver calculation itself, but rendering the state transition diagram requires external loading of Mermaid. Display and operation on GitHub Pages have not been verified this time. Even if the diagram is not displayed, you can check the boundaries using the table in the text and the Node.js example.
Reading the Three Numbers of SemVer
The basic format of SemVer (Semantic Versioning) is メジャー.マイナー.パッチ. The specification intends that breaking changes increment the major version, backward-compatible feature additions increment the minor version, and backward-compatible bug fixes increment the patch version. However, this does not guarantee that the package author actually follows this convention ^ itself.SemVer 2.0.0 specificationshould also be reviewed.
In npm dependency specifications, ^(caret) represents the range that does not change the leftmost non-zero digit. Examples using all three digits from node-semver's official Caret Ranges documentationare listed below.
| Specified | How to Read Ranges | Examples within range | Examples out of range |
|---|---|---|---|
^1.2.3 | >=1.2.3 <2.0.0-0 | 1.2.9、1.9.0 | 1.2.2、2.0.0 |
^0.2.3 | >=0.2.3 <0.3.0-0 | 0.2.9 | 0.2.2、0.3.0 |
^0.0.3 | >=0.0.3 <0.0.4-0 | 0.0.3 | 0.0.2、0.0.4 |
0.2.3, since the major version is 0, the next non-zero number, which is the minor 2, determines the upper limit.0.0.3, the patch version is fixed up to 3, and the next patch 0.0.4 is also out of range. This is not a uniform rule where "0.x always restricts to patch only"; rather, the upper limit is evaluated based on the numbers of the specified version.
~1.2.3 (tilde) is >=1.2.3 <1.3.0-0. Therefore, 1.2.9 falls into both ^1.2.3 and ~1.2.3, while 1.3.0 falls only into the former. The difference lies in whether minor updates are allowed or limited to patch updates. Pre-releases with upper limit identifiers like 1.3.0-beta.1 are out of range for both specifications.
Evaluating Pre-releases Separately
1.3.0-beta.1Versions such as include a pre-release identifier indicating they are before the official release. By default in node-semver, even if they appear to fall within a standard version range, they are not included in the range unless a pre-release is explicitly specified in the corresponding comparison condition for major.minor.patch. Therefore, for ^1.2.3 against 1.3.0-beta.1 is out of range, and 1.3.0 is within range.
On the other hand, if the range is set to ^1.2.3-beta.2, the same 1.2.3 series 1.2.3-beta.4 satisfies the condition, but a different series 1.2.4-beta.1 does not. When using a beta version, do not evaluate it with the same assumptions as the stable version; instead, check the node-semver pre-release rules.
Evaluating in Node.js
To try this locally, prepare Node.js 18 or later, as the accompanying tests use the built-in node:test. Create a working folder, pin the semver version, and install it.
mkdir semver-lab cd semver-lab npm init -y npm install --save-exact semver@7.8.5
Save the following as demo.cjs. Range errors and candidate formatting errors are displayed separately.
const semver = require('semver');
const range = process.argv[2] ?? '^1.2.3';
const candidates = process.argv.slice(3);
const normalizedRange = semver.validRange(range);
if (!normalizedRange) {
console.error('範囲指定を確認してください:', range);
process.exitCode = 2;
} else if (candidates.length === 0) {
console.error('候補のバージョンを1つ以上指定してください。');
process.exitCode = 2;
} else {
console.table(candidates.map(version => ({
version,
result: !semver.valid(version)
? 'バージョン形式が不正'
: semver.satisfies(version, normalizedRange) ? '範囲内' : '範囲外',
})));
}
Run the following commands in order to verify the table boundaries.
node demo.cjs "^1.2.3" 1.2.2 1.2.3 1.9.0 2.0.0 node demo.cjs "^0.2.3" 0.2.2 0.2.3 0.2.9 0.3.0 node demo.cjs "^0.0.3" 0.0.2 0.0.3 0.0.4 node demo.cjs "^1.2.3" 1.3.0-beta.1 1.3.0
In the execution results, it is as expected if versions lower than the starting point and versions greater than or equal to the upper limit are marked as "out of range", and stable versions between the starting point and below the upper limit are marked as "within range". If the range is invalid, an error occurs with exit code 2. If a candidate format is invalid, only that candidate displays "invalid version format".
package.jsonSeparating the version range from the actual installed version
^1.2.3 is an allowable range and is not a record that "that version is currently installed." In the official npm package.json dependency specification, npm installhandles packages that satisfy dependency requirements. Furthermore, as described inExplanation of npm install and package-lock.json, if the lock file version satisfies the range, npm uses that pinned version to maintain reproducibility.
When checking in an existing project, review them in the following order:
package.jsonto check what range is specified for the target package.npm ls パッケージ名to inspect the version actually installed in the working environment.package-lock.jsonto understand the resolved version pinned across the team.When updating, run
npm update パッケージ名on a branch and review the lock file diff and test results. While npm update respects SemVer requirements for updates, as stated in theofficial documentation for npm update, it does not rewrite the package.json ranges by default.
Just because a version falls within the range does not mean updates can be applied directly to production. Especially since 0.x may be treated as versions before API stability, consider the possibility that breaking changes can occur even in patch updates.^does not replace the author's compatibility policy, so please verify through the dependency's change log and your own tests.
When troubleshooting fails
Cannot find module 'semver':npm installRunnode demo.cjs ...within the working folder where you performed the operation.node_modulesIf there is no , check whether the installation has completed.Range specification error:
^Check whether full-width symbols were used instead of half-width, or if the range was enclosed in quotation marks.Unexpected candidate within range: Read the candidate versions and specified range one by one, and check whether they are pre-release versions.
Installed version is not the latest in the range:
package-lock.jsonmay retain a fixed version that satisfies the range.npm lsand check the lockfile first.
Summary
^The upper limit of is determined by the leftmost non-zero digit, and^1.2.3、^0.2.3、^0.0.3varies by. Pre-releases are evaluated under different conditions than standard versions. Finally, by separately checking the range specification, the version recorded in the lockfile, and the locally installed version, you can reduce oversight in dependency updates.
Official and Primary Sources
The Node.js code in the text has been verified for execution. GitHub Pages rendering for the live demo, WordPress rendering of the article, and update operations on actual projects were not verified this time. Please test with fictitious versions rather than actual data.
This article was created using AI.

