Skip to content

Check for a .nvmrc or .node-version file to determine the Node.js version #1374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/node/devcontainer-feature.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "node",
"version": "1.6.2",
"version": "1.6.3",
"name": "Node.js (via nvm), yarn and pnpm",
"documentationURL": "https://github.com/devcontainers/features/tree/main/src/node",
"description": "Installs Node.js, nvm, yarn, pnpm, and needed dependencies.",
Expand All @@ -13,7 +13,8 @@
"none",
"18",
"16",
"14"
"14",
"project-file" // This will read the Node version from .node-version or .nvmrc in the project
],
"default": "lts",
"description": "Select or enter a Node.js version to install"
Expand Down
24 changes: 23 additions & 1 deletion src/node/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export NVM_VERSION="${NVMVERSION:-"latest"}"
export NVM_DIR="${NVMINSTALLPATH:-"/usr/local/share/nvm"}"
INSTALL_TOOLS_FOR_NODE_GYP="${NODEGYPDEPENDENCIES:-true}"
export INSTALL_YARN_USING_APT="${INSTALLYARNUSINGAPT:-true}" # only concerns Debian-based systems

# Comma-separated list of node versions to be installed (with nvm)
# alongside NODE_VERSION, but not set as default.
ADDITIONAL_VERSIONS="${ADDITIONALVERSIONS:-""}"
Expand Down Expand Up @@ -287,6 +286,29 @@ if ! type git > /dev/null 2>&1; then
check_packages git
fi

# Determine the Node.js version using the .nvmrc or .node-version file if present.
if [[ "${NODE_VERSION}" == "project-file" ]]; then
echo "Finding Node version from .nvmrc or .node-version file..."
NODE_VERSION_FOUND=""
for version_file in ".node-version" ".nvmrc"; do
if [[ -f "$version_file" && -s "$version_file" ]]; then
file_version=$(tr -d '[:space:]' < "$version_file")
if [[ -n "$file_version" ]]; then
echo "Using Node version from $version_file: $file_version"
NODE_VERSION="$file_version"
NODE_VERSION_FOUND="yes"
break
else
echo "$version_file exists but contains only whitespace. Continuing search..."
fi
fi
done
if [[ -z "$NODE_VERSION_FOUND" ]]; then
NODE_VERSION="lts"
echo "No Node version found in .nvmrc or .node-version. Using default: lts"
fi
fi

# Adjust node version if required
if [ "${NODE_VERSION}" = "none" ]; then
export NODE_VERSION=
Expand Down
15 changes: 12 additions & 3 deletions test/node/install_additional_node.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@ set -e
# Optional: Import test library
source dev-container-features-test-lib

# 'lts' is now some version of node 20...
check "version_on_path" node -v | grep 20
# Install jq if not already installed
if ! type jq >/dev/null 2>&1; then
apt-get update && apt-get install -y jq
fi

#Get the latest LTS version of Node.js
LATEST_LTS_VERSION=$(curl -s https://nodejs.org/dist/index.json | jq -r '[.[] | select(.lts != false)][0].version')


# 'lts' is fetched instead of hardcoded to a specific version
check "version_on_path" node -v | grep "$LATEST_LTS_VERSION"
check "pnpm" bash -c "pnpm -v | grep 8.8.0"

check "v20_installed" ls -1 /usr/local/share/nvm/versions/node | grep 20
check "lts_installed" ls -1 /usr/local/share/nvm/versions/node | grep "$LATEST_LTS_VERSION"
check "v14_installed" ls -1 /usr/local/share/nvm/versions/node | grep 14.19.3
check "v17_installed" ls -1 /usr/local/share/nvm/versions/node | grep 17.9.1

Expand Down
14 changes: 11 additions & 3 deletions test/node/install_additional_node_on_rhel_family.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,19 @@ set -e
# Optional: Import test library
source dev-container-features-test-lib

# 'lts' is now some version of node 20...
check "version_on_path" node -v | grep 20
# Install jq if not already installed
if ! type jq >/dev/null 2>&1; then
yum install -y jq
fi

#Get the latest LTS version of Node.js
LATEST_LTS_VERSION=$(curl -s https://nodejs.org/dist/index.json | jq -r '[.[] | select(.lts != false)][0].version')

# 'lts' is fetched instead of hardcoded to a specific version
check "version_on_path" node -v | grep "$LATEST_LTS_VERSION"
check "pnpm" bash -c "pnpm -v | grep 6.16.0"

check "v20_installed" ls -1 /usr/local/share/nvm/versions/node | grep 20
check "lts_installed" ls -1 /usr/local/share/nvm/versions/node | grep "$LATEST_LTS_VERSION"
check "v14_installed" ls -1 /usr/local/share/nvm/versions/node | grep 14.19.3
check "v17_installed" ls -1 /usr/local/share/nvm/versions/node | grep 17.9.1

Expand Down
32 changes: 31 additions & 1 deletion test/node/scenarios.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"features": {
"node": {
"version": "16",
"pnpmVersion":"8.8.0"
"pnpmVersion": "8.8.0"
}
}
},
Expand Down Expand Up @@ -199,5 +199,35 @@
"installYarnUsingApt": false
}
}
},
"test_node_project_nvmrc": {
"build": {
"dockerfile": "DockerFile"
},
"features": {
"node": {
"version": "project-file"
}
}
},
"test_node_project_nodev": {
"build": {
"dockerfile": "DockerFile"
},
"features": {
"node": {
"version": "project-file"
}
}
},
"test_node_project_nvm_nodev": {
"build": {
"dockerfile": "DockerFile"
},
"features": {
"node": {
"version": "project-file"
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason why we are adding these test cases such that the container is built from Dockerfile only & not using the image tag directly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes @Kaniska244
Using Docker File I can include the .nvmrc (or .node-version) file in the image during the build process which is then gets used in feature.

}
}
27 changes: 27 additions & 0 deletions test/node/test_node_project_nodev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

cd test_node_project_nodev

# Check that .nvmrc exists
if [ ! -f .node-version ]; then
echo ".node-version file not found!"
exit 1
fi

# Read the version from .nvmrc and compare with current node version
N_VERSION=$(cat .node-version | tr -d 'v')
NODE_VERSION=$(node -v | tr -d 'v')

if [ "$N_VERSION" != "$NODE_VERSION" ]; then
echo "Node version mismatch: .node-version specifies $N_VERSION, but current node is $NODE_VERSION"
exit 1
fi

echo ".node-version is used and matches the current Node.js version."
# Report result
reportResults
1 change: 1 addition & 0 deletions test/node/test_node_project_nodev/.node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.19.2
3 changes: 3 additions & 0 deletions test/node/test_node_project_nodev/DockerFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM debian:11

COPY .node-version /tmp/dev-container-features/node_0/.node-version
27 changes: 27 additions & 0 deletions test/node/test_node_project_nvm_nodev.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

cd test_node_project_nvm_nodev

# Check that .node-version exists
if [ ! -f .node-version ]; then
echo ".node-version file not found!"
exit 1
fi

# Read the version from .node-version and compare with current node version
N_VERSION=$(cat .node-version | tr -d 'v')
NODE_VERSION=$(node -v | tr -d 'v')

if [ "$N_VERSION" != "$NODE_VERSION" ]; then
echo "Node version mismatch: .nvmrc specifies $N_VERSION, but current node is $NODE_VERSION"
exit 1
fi

echo ".node-version is used and matches the current Node.js version."
# Report result
reportResults
1 change: 1 addition & 0 deletions test/node/test_node_project_nvm_nodev/.node-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.19.2
1 change: 1 addition & 0 deletions test/node/test_node_project_nvm_nodev/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22.9.0
4 changes: 4 additions & 0 deletions test/node/test_node_project_nvm_nodev/DockerFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM debian:11

COPY .nvmrc .node-version /tmp/dev-container-features/node_0/

27 changes: 27 additions & 0 deletions test/node/test_node_project_nvmrc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

set -e

# Optional: Import test library
source dev-container-features-test-lib

cd test_node_project_nvmrc

# Check that .nvmrc exists
if [ ! -f .nvmrc ]; then
echo ".nvmrc file not found!"
exit 1
fi

# Read the version from .nvmrc and compare with current node version
NVMRC_VERSION=$(cat .nvmrc | tr -d 'v')
NODE_VERSION=$(node -v | tr -d 'v')

if [ "$NVMRC_VERSION" != "$NODE_VERSION" ]; then
echo "Node version mismatch: .nvmrc specifies $NVMRC_VERSION, but current node is $NODE_VERSION"
exit 1
fi

echo ".nvmrc is used and matches the current Node.js version."
# Report result
reportResults
1 change: 1 addition & 0 deletions test/node/test_node_project_nvmrc/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22.9.0
3 changes: 3 additions & 0 deletions test/node/test_node_project_nvmrc/DockerFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM debian:11

COPY .nvmrc /tmp/dev-container-features/node_0/.nvmrc