You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This action determines which Node.js version to use in your workflow. It's designed for use in centralized workflows where users need to control Node.js versions from their repository without modifying the workflow.
4
+
5
+
Maintainers can specify Node.js version via repository variable or `package.json` and `engines.node` value.
6
+
7
+
## How It Works
8
+
9
+
```mermaid
10
+
flowchart TD
11
+
Start([Action Starts]) --> CheckEnv{NODE_VERSION<br/>env variable set in repo settings?}
12
+
CheckEnv -->|Yes| UseEnv[Use NODE_VERSION value]
13
+
CheckEnv -->|No| CheckPackageJson{package.json<br/>exists in root?}
Users have two options to specify their desired Node.js version:
66
+
67
+
### Option 1: Repository Variable (Easiest)
68
+
69
+
Set a `NODE_VERSION` repository variable in GitHub:
70
+
1. Go to repository **Settings → Secrets and variables → Actions → Variables tab**
71
+
2. Click **New repository variable**
72
+
3. Name: `NODE_VERSION`
73
+
4. Value: `20`or `22` or any Node.js major version
74
+
5. Click **Add variable**
75
+
76
+
### Option 2: package.json engines field
77
+
78
+
Add the `engines.node` field to `package.json` with a semver range:
79
+
80
+
```json
81
+
{
82
+
"name": "my-project",
83
+
"version": "1.0.0",
84
+
"engines": {
85
+
"node": ">=20.0.0"
86
+
}
87
+
}
88
+
```
89
+
90
+
The action uses the **semver** package to properly parse complex version ranges and automatically selects the **highest even-numbered (LTS) major version** that satisfies the range:
91
+
92
+
- `>=20.0.0`→ `20`
93
+
- `>=18.0.0 <22`→ `18`
94
+
- `>=18.10.3 <22`→ `18`
95
+
- `20.x`→ `20`
96
+
97
+
**Note:** The action only considers LTS versions (even major numbers: 14, 16, 18, 20, 22, 24, etc.) and returns the major version number. The `actions/setup-node` action will automatically select the appropriate patch version.
98
+
99
+
## Outputs
100
+
101
+
| Output | Description |
102
+
|--------|-------------|
103
+
| `version` | Node.js version number to use with `actions/setup-node` |
Copy file name to clipboardExpand all lines: .github/actions/get-node-version-from-package-lock/action.yml
+69-3Lines changed: 69 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,85 @@
1
1
name: 'Specify Node version based on package-lock file version'
2
-
description: 'This action reads package-lock.json file and determines Node.js version to use with actions/setup-node action'
2
+
description: 'This action reads package-lock.json file and determines Node.js version to use with actions/setup-node action. Can be overridden via NODE_VERSION repository variable or engines.node in package.json'
3
+
inputs:
4
+
node-version:
5
+
description: 'Node.js version override (e.g. from vars.NODE_VERSION)'
6
+
required: false
3
7
outputs:
4
8
version:
5
9
description: 'Node.js version number to use with actions/setup-node action'
6
10
value: ${{ steps.getnode.outputs.version }}
7
11
runs:
8
12
using: "composite"
9
13
steps:
14
+
- name: Install semver package
15
+
shell: bash
16
+
run: npm install --no-save semver@7.7.3
17
+
10
18
- name: Get Node version
11
19
uses: actions/github-script@v7
12
20
id: getnode
21
+
env:
22
+
# Map the composite action input to an environment variable for the script
0 commit comments