Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 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
29 changes: 28 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,34 @@ 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_PATH=$(find . -type f -name ".node-version" | head -n 1)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure find is the best option. Can we not assume the files need to be in the root directory? What if the project is huge, find would take a long time. if there are multiple of these files, find might order them differently based on where it is running, so something like:

  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"
                break
            else
                echo "$version_file exists but contains only whitespace. Continuing search..."
            fi
        fi
    done

might work more predictably

NVMRC_PATH=$(find . -type f -name ".nvmrc" | head -n 1)
# Used as the default when no file exists or if the file is empty
NODE_VERSION="lts"
if [ -n "$NODE_VERSION_PATH" ]; then
NODE_VERSION_NODE_VERSION_FILE=$(<"$NODE_VERSION_PATH" xargs)
if [ -n "$NODE_VERSION_NODE_VERSION_FILE" ]; then
echo "Using Node version from .node-version file in $NODE_VERSION_PATH: $NODE_VERSION_NODE_VERSION_FILE"
NODE_VERSION="${NODE_VERSION_NODE_VERSION_FILE}"
else
echo "$NODE_VERSION_PATH file is empty. No Node version specified. Using the default: ${NODE_VERSION}."
fi
elif [ -n "$NVMRC_PATH" ]; then
NODE_VERSION_NVMRC=$(<"$NVMRC_PATH" xargs)
if [ -n "$NODE_VERSION_NVMRC" ]; then
echo "Using Node version from .nvmrc file in $NVMRC_PATH: $NODE_VERSION_NVMRC"
NODE_VERSION="${NODE_VERSION_NVMRC}"
else
echo "$NVMRC_PATH file is empty. No Node version specified. Using the default: ${NODE_VERSION}."
fi
else
echo "No .node-version or .nvmrc file found. Using the default Node version: ${NODE_VERSION}."
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
Copy Markdown
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
Copy Markdown
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/sample-node

# 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
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 sample-node /tmp/dev-container-features/node_0/sample-node
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_nodev/sample-node/src/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello from Node.js!');
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/sample-node

# 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
3 changes: 3 additions & 0 deletions test/node/test_node_project_nvm_nodev/DockerFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM debian:11

COPY sample-node /tmp/dev-container-features/node_0/sample-node
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/sample-node/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22.9.0
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello from Node.js!');
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/sample-node

# 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
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 sample-node /tmp/dev-container-features/node_0/sample-node
1 change: 1 addition & 0 deletions test/node/test_node_project_nvmrc/sample-node/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
22.9.0
1 change: 1 addition & 0 deletions test/node/test_node_project_nvmrc/sample-node/src/hello.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello from Node.js!');