Skip to content

Commit 4225024

Browse files
Rebuild Node/Nodesource Feature
Now requires a specific semver version string which improves overall reliability and clarity in what Node.js version is being installed. The default “automatic” will install the latest Node.js v24 (current LTS release) and generally shouldn’t be used, but if you _really_ don’t care, you can use the default. Also, run `chown` during the `onCreateCommand` lifecycle hook to free up relying Dev Containers to use the `updateContentCommand` hook.
1 parent 3d593d3 commit 4225024

File tree

5 files changed

+61
-22
lines changed

5 files changed

+61
-22
lines changed

src/node/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ Install [Node.js](https://nodejs.org) from [Nodesource's DEB repository](https:/
66

77
```json
88
"features": {
9-
"ghcr.io/CargoSense/devcontainer-features/node:2": {}
9+
"ghcr.io/CargoSense/devcontainer-features/node:3": {}
1010
}
1111
```
1212

1313
## Options
1414

1515
| Option ID | Description | Type | Default Value |
1616
|:----------|:--------------------------------|:-------|:--------------|
17-
| `version` | The Node.js version to install. | string | `24` |
17+
| `version` | The Node.js version to install. | string | `automatic` |
1818

1919
## OS Support
2020

src/node/devcontainer-feature.json

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
{
22
"name": "Node.js",
33
"id": "node",
4-
"version": "2.0.0",
4+
"version": "3.0.0",
55
"description": "Install Node.js from Nodesource's DEB repository.",
66
"options": {
77
"version": {
88
"type": "string",
9-
"enum": [
10-
"20",
11-
"21",
12-
"22",
13-
"23",
14-
"24",
15-
"25"
16-
],
17-
"default": "24",
18-
"description": "Select or enter a Node.js version."
9+
"default": "automatic",
10+
"description": "The Node.js version to install."
1911
}
2012
},
2113
"containerEnv": {
@@ -36,5 +28,5 @@
3628
"type": "volume"
3729
}
3830
],
39-
"postCreateCommand": "sudo chown -R ${USER}:${USER} /usr/local/npm ${containerWorkspaceFolder}/node_modules"
31+
"onCreateCommand": "sudo chown -R ${USER}:${USER} /usr/local/npm ${containerWorkspaceFolder}/node_modules"
4032
}

src/node/install.sh

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,70 @@
1-
#!/usr/bin/env sh
1+
#!/usr/bin/env bash
22

33
set -e
44

5-
NODE_VERSION="${VERSION:-"24"}"
5+
NODE_VERSION="${VERSION:-"automatic"}"
6+
NODE_MAJOR_VERSION="24"
7+
8+
pkg="nodejs"
9+
10+
if [ "${NODE_VERSION}" != "automatic" ]; then
11+
NODE_MAJOR_VERSION="$(echo "${NODE_VERSION}" | cut -d. -f1)"
12+
pkg="${pkg}=${NODE_VERSION}-1nodesource1"
13+
fi
614

715
curl_installed=""
16+
gpg_installed=""
817

918
if ! type curl >/dev/null 2>&1; then
1019
apt update --yes
11-
apt install --no-install-recommends --yes curl ca-certificates
20+
apt install --yes curl
1221

1322
curl_installed="true"
1423
fi
1524

16-
curl -fsSL https://deb.nodesource.com/setup_"${NODE_VERSION}".x | bash -
25+
if ! type gpg >/dev/null 2>&1; then
26+
apt update --yes
27+
apt install --yes gnupg
1728

18-
apt update --yes
19-
apt install --no-install-recommends --yes nodejs
29+
gpg_installed="true"
30+
fi
31+
32+
apt_sources_snippet="$(cat << EOF
33+
Types: deb
34+
URIs: https://deb.nodesource.com/node_${NODE_MAJOR_VERSION}.x
35+
Suites: nodistro
36+
Components: main
37+
Signed-By: /etc/apt/keyrings/nodesource.gpg
38+
EOF
39+
)"
40+
41+
install -dm 755 /etc/apt/keyrings
42+
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
43+
echo "${apt_sources_snippet}" | tee /etc/apt/sources.list.d/nodesource.sources
2044

2145
if [ -n "${curl_installed}" ]; then
2246
apt purge curl --autoremove --yes
47+
rm -rf /var/lib/apt/lists/*
48+
fi
49+
50+
if [ -n "${gpg_installed}" ]; then
51+
apt purge gnupg --autoremove --yes
52+
rm -rf /var/lib/apt/lists/*
53+
fi
54+
55+
apt update --yes
56+
apt install --yes "${pkg}"
57+
rm -rf /var/lib/apt/lists/*
58+
59+
node_rc_snippet="$(cat << EOF
60+
export NODE_VERSION="$(node -v | cut -c2-)"
61+
EOF
62+
)"
63+
64+
if [[ "$(cat /etc/bash.bashrc)" != *"${node_rc_snippet}"* ]]; then
65+
echo "${node_rc_snippet}" >> /etc/bash.bashrc
66+
fi
67+
68+
if [ -f "/etc/zsh/zshrc" ] && [[ "$(cat /etc/zsh/zshrc)" != *"${node_rc_snippet}"* ]]; then
69+
echo "${node_rc_snippet}" >> /etc/zsh/zshrc
2370
fi

test/node/node-20.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set -e
77
source dev-container-features-test-lib
88

99
# Feature-specific tests
10-
check "version" bash -c "node --version | grep -E 'v20\..+'"
10+
check "version" bash -c "node --version | grep v20.1.0"
1111

1212
# Report result
1313
reportResults

test/node/scenarios.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"image": "mcr.microsoft.com/devcontainers/base:debian",
44
"features": {
55
"node": {
6-
"version": "20"
6+
"version": "20.1.0"
77
}
88
}
99
}

0 commit comments

Comments
 (0)