Node.js Engines Field — Specifying Version Ranges
How to use semver ranges in the engines field of package.json to specify Node.js version requirements. Supports >=, ||, and LTS targeting.
Detailed Explanation
Node.js Engine Version Ranges
The engines field in package.json lets you specify which versions of Node.js your project supports. Package managers can warn or error when the wrong version is used.
Basic Syntax
{
"engines": {
"node": ">=18.0.0"
}
}
Common Patterns
| Requirement | Range | Description |
|---|---|---|
| Any Node 18+ | >=18.0.0 |
Minimum version only |
| Node 18 or 20 | ^18.0.0 || ^20.0.0 |
LTS versions only |
| Node 18-20 | >=18.0.0 <21.0.0 |
Bounded range |
| Exact LTS | ^20.0.0 |
Any Node 20.x |
| Current + LTS | ^18.0.0 || ^20.0.0 || ^22.0.0 |
Multiple LTS lines |
Enforcement
By default, npm only warns about engine mismatches. To make it an error:
# In .npmrc
engine-strict=true
Or run:
npm config set engine-strict true
Including npm Version
You can also specify the npm version:
{
"engines": {
"node": ">=18.0.0",
"npm": ">=9.0.0"
}
}
Targeting LTS Only
Node.js releases even-numbered major versions as LTS. To support only LTS:
{
"engines": {
"node": "^18.0.0 || ^20.0.0 || ^22.0.0"
}
}
CI/CD Considerations
Your engine range should match your CI test matrix. If you test on Node 18 and 20, your engines field should reflect that:
{
"engines": {
"node": "^18.0.0 || ^20.0.0"
}
}
This ensures you do not claim support for versions you do not test against.
Use Case
Configuring package.json to enforce Node.js version requirements across a team or in CI/CD, ensuring consistent runtime environments.
Try It — Semver Calculator
Related Topics
OR (||) Union Ranges — Combining Multiple Ranges
Union Ranges
Comparison Operators (>=, >, <, <=, =) in Semver Ranges
Comparison Operators
Caret Range (^) — How It Works
Caret Ranges
Check Version Compatibility — Does Your Dependency Work?
Compatibility
npm Version Range Cheat Sheet — All Operators Explained
Reference