From b3aa44651e0cd165ebba3af682dccde716b5f00f Mon Sep 17 00:00:00 2001 From: sireeshajonnalagadda Date: Tue, 7 Apr 2026 06:05:06 +0000 Subject: [PATCH 1/2] feat(node): add npm version selection and installation options --- src/node/devcontainer-feature.json | 16 +++++++++- src/node/install.sh | 36 +++++++++++++++++++++ test/node/install_npm_latest.sh | 15 +++++++++ test/node/install_npm_none.sh | 12 +++++++ test/node/install_specific_npm_version.sh | 12 +++++++ test/node/scenarios.json | 38 ++++++++++++++++++++--- 6 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 test/node/install_npm_latest.sh create mode 100644 test/node/install_npm_none.sh create mode 100644 test/node/install_specific_npm_version.sh diff --git a/src/node/devcontainer-feature.json b/src/node/devcontainer-feature.json index c8ceb966f..637bd06fc 100644 --- a/src/node/devcontainer-feature.json +++ b/src/node/devcontainer-feature.json @@ -27,6 +27,20 @@ "default": "/usr/local/share/nvm", "description": "The path where NVM will be installed." }, + "npmVersion": { + "type": "string", + "proposals": [ + "10.9.0", + "10.8.0", + "10.7.0", + "9.9.3", + "8.19.4", + "latest", + "none" + ], + "default": "10.9.0", + "description": "Select or enter a specific NPM version to install globally. Use 'latest' for the latest version, 'none' to skip npm version update, or specify a version like '10.9.0'." + }, "pnpmVersion": { "type": "string", "proposals": [ @@ -78,4 +92,4 @@ "installsAfter": [ "ghcr.io/devcontainers/features/common-utils" ] -} +} \ No newline at end of file diff --git a/src/node/install.sh b/src/node/install.sh index 1d89abd0a..764e30c13 100755 --- a/src/node/install.sh +++ b/src/node/install.sh @@ -8,6 +8,7 @@ # Maintainer: The Dev Container spec maintainers export NODE_VERSION="${VERSION:-"lts"}" +export NPM_VERSION="${NPMVERSION:-"lts"}" export PNPM_VERSION="${PNPMVERSION:-"latest"}" export NVM_VERSION="${NVMVERSION:-"latest"}" export NVM_DIR="${NVMINSTALLPATH:-"/usr/local/share/nvm"}" @@ -381,6 +382,41 @@ if [ ! -z "${ADDITIONAL_VERSIONS}" ]; then IFS=$OLDIFS fi +# Install or update npm to specific version +if [ ! -z "${NPM_VERSION}" ] && [ "${NPM_VERSION}" = "none" ]; then + echo "Ignoring NPM version update" +else + if bash -c ". '${NVM_DIR}/nvm.sh' && type npm >/dev/null 2>&1"; then + ( + . "${NVM_DIR}/nvm.sh" + [ ! -z "$http_proxy" ] && npm set proxy="$http_proxy" + [ ! -z "$https_proxy" ] && npm set https-proxy="$https_proxy" + [ ! -z "$no_proxy" ] && npm set noproxy="$no_proxy" + echo "Installing npm version ${NPM_VERSION}..." + + # Clear npm cache to avoid conflicts + npm cache clean --force 2>/dev/null || true + + # Try npm installation with retries + for i in {1..3}; do + if npm install -g npm@$NPM_VERSION --force; then + echo "Successfully installed npm@${NPM_VERSION}" + break + else + echo "Attempt $i failed, retrying..." + sleep 2 + if [ $i -eq 3 ]; then + echo "Failed to install npm@${NPM_VERSION} after 3 attempts. Trying latest npm as fallback..." + npm install -g npm@latest --force || echo "Fallback to latest npm also failed. Keeping current npm version $(npm --version 2>/dev/null || echo 'unknown')." + fi + fi + done + ) + else + echo "Skip installing/updating npm because npm is not available" + fi +fi + # Install pnpm if [ ! -z "${PNPM_VERSION}" ] && [ "${PNPM_VERSION}" = "none" ]; then echo "Ignoring installation of PNPM" diff --git a/test/node/install_npm_latest.sh b/test/node/install_npm_latest.sh new file mode 100644 index 000000000..4272b900a --- /dev/null +++ b/test/node/install_npm_latest.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +set -e + +# Optional: Import test library +source dev-container-features-test-lib + +# Verify npm is latest version (valid version format) +check "npm_latest_version" bash -c "npm -v | grep -E '^[0-9]+\.[0-9]+\.[0-9]+'" + +# Also verify pnpm works as configured +check "pnpm_version" bash -c "pnpm -v | grep 8.8.0" + +# Report result +reportResults \ No newline at end of file diff --git a/test/node/install_npm_none.sh b/test/node/install_npm_none.sh new file mode 100644 index 000000000..029718489 --- /dev/null +++ b/test/node/install_npm_none.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -e + +# Optional: Import test library +source dev-container-features-test-lib + +# When npmVersion is "none", npm should not be updated from node's bundled version +check "npm_not_updated" bash -c "npm --version" + +# Report result +reportResults \ No newline at end of file diff --git a/test/node/install_specific_npm_version.sh b/test/node/install_specific_npm_version.sh new file mode 100644 index 000000000..8c541fd07 --- /dev/null +++ b/test/node/install_specific_npm_version.sh @@ -0,0 +1,12 @@ +#!/bin/bash + +set -e + +# Optional: Import test library +source dev-container-features-test-lib + +# Verify npm is installed with specific version 10.8.0 +check "npm_specific_version" bash -c "npm -v | grep '^10.8.0'" + +# Report result +reportResults \ No newline at end of file diff --git a/test/node/scenarios.json b/test/node/scenarios.json index e3b4297a1..4cc5084dd 100644 --- a/test/node/scenarios.json +++ b/test/node/scenarios.json @@ -6,7 +6,7 @@ "version": "lts" } } - }, + }, "install_node_debian_bookworm": { "image": "debian:12", "features": { @@ -14,7 +14,7 @@ "version": "lts" } } - }, + }, "nvm_test_fallback": { "image": "debian:11", "features": { @@ -22,7 +22,7 @@ "version": "lts" } } - }, + }, "install_additional_node": { "image": "debian:11", "features": { @@ -98,7 +98,7 @@ "features": { "node": { "version": "22", - "pnpmVersion":"8.8.0" + "pnpmVersion": "8.8.0" } } }, @@ -207,5 +207,33 @@ "version": "lts" } } + }, + "install_specific_npm_version": { + "image": "debian:12", + "features": { + "node": { + "version": "lts", + "npmVersion": "10.8.0" + } + } + }, + "install_npm_none": { + "image": "mcr.microsoft.com/devcontainers/base", + "features": { + "node": { + "version": "lts", + "npmVersion": "none" + } + } + }, + "install_npm_latest": { + "image": "debian:12", + "features": { + "node": { + "version": "22", + "npmVersion": "latest", + "pnpmVersion": "8.8.0" + } + } } -} +} \ No newline at end of file From f78d73f1ee57b3ff34e75fbea4ee2ad8a856fe18 Mon Sep 17 00:00:00 2001 From: sireeshajonnalagadda Date: Tue, 7 Apr 2026 08:00:46 +0000 Subject: [PATCH 2/2] add 'lts' and 'latest' options for npm version selection --- src/node/devcontainer-feature.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/node/devcontainer-feature.json b/src/node/devcontainer-feature.json index 637bd06fc..c0d79c93b 100644 --- a/src/node/devcontainer-feature.json +++ b/src/node/devcontainer-feature.json @@ -30,6 +30,8 @@ "npmVersion": { "type": "string", "proposals": [ + "lts", + "latest", "10.9.0", "10.8.0", "10.7.0",