Skip to content
This repository was archived by the owner on May 15, 2025. It is now read-only.
Merged
1 change: 1 addition & 0 deletions .icons/node.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
display_name: nodejs
description: Install Node.js via nvm
icon: ../.icons/node.svg
maintainer_github: TheZoker
verified: false
tags: [helper]
---

# nodejs

Automatically installs [Node.js](https://github.com/nodejs/node) via [nvm](https://github.com/nvm-sh/nvm). It can also install multiple versions of node and set a default version. If no options are specified, the latest version is installed.

```tf
module "nodejs" {
source = "registry.coder.com/modules/nodejs/coder"
version = "1.0.2"
agent_id = coder_agent.example.id
}
```

### Install multiple versions

This installs multiple versions of Node.js:

```tf
module "nodejs" {
source = "registry.coder.com/modules/nodejs/coder"
version = "1.0.2"
agent_id = coder_agent.example.id
node_versions = [
"18",
"20",
"node"
]
default_node_version = "20"
}
```

### Full example

A example with all available options:

```tf
module "nodejs" {
source = "registry.coder.com/modules/nodejs/coder"
version = "1.0.2"
agent_id = coder_agent.example.id
nvm_version = "v0.39.7"
nvm_install_prefix = "/opt/nvm"
node_versions = [
"16",
"18",
"node"
]
default_node_version = "16"
}
```
12 changes: 12 additions & 0 deletions nodejs/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { describe, expect, it } from "bun:test";
import { runTerraformInit, testRequiredVariables } from "../test";

describe("nodejs", async () => {
await runTerraformInit(import.meta.dir);

testRequiredVariables(import.meta.dir, {
agent_id: "foo",
});

// More tests depend on shebang refactors
});
52 changes: 52 additions & 0 deletions nodejs/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
terraform {
required_version = ">= 1.0"

required_providers {
coder = {
source = "coder/coder"
version = ">= 0.12"
}
}
}

variable "agent_id" {
type = string
description = "The ID of a Coder agent."
}

variable "nvm_version" {
type = string
description = "The version of nvm to install."
default = "master"
}

variable "nvm_install_prefix" {
type = string
description = "The prefix to install nvm to."
default = "$HOME/.nvm"
}

variable "node_versions" {
type = list(string)
description = "A list of Node.js versions to install."
default = ["node"]
}

variable "default_node_version" {
type = string
description = "The default Node.js version"
default = "node"
}

resource "coder_script" "nodejs" {
agent_id = var.agent_id
display_name = "Node.js:"
script = templatefile("${path.module}/run.sh", {
NVM_VERSION : var.nvm_version,
INSTALL_PREFIX : var.nvm_install_prefix,
NODE_VERSIONS : join(",", var.node_versions),
DEFAULT : var.default_node_version,
})
run_on_start = true
start_blocks_login = true
}
50 changes: 50 additions & 0 deletions nodejs/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env bash

NVM_VERSION='${NVM_VERSION}'
NODE_VERSIONS='${NODE_VERSIONS}'
INSTALL_PREFIX='${INSTALL_PREFIX}'
DEFAULT='${DEFAULT}'
BOLD='\033[0;1m'
CODE='\033[36;40;1m'
RESET='\033[0m'

printf "$${BOLD}Installing nvm!$${RESET}\n"

export NVM_DIR="$${INSTALL_PREFIX}/nvm"

script="$(curl -sS -o- "https://raw.githubusercontent.com/nvm-sh/nvm/$${NVM_VERSION}/install.sh" 2>&1)"
if [ $? -ne 0 ]; then
echo "Failed to download nvm installation script: $script"
exit 1
fi

output="$(bash <<< "$script" 2>&1)"
if [ $? -ne 0 ]; then
echo "Failed to install nvm: $output"
exit 1
fi

printf "🥳 nvm has been installed\n\n"

# Set up nvm for the rest of the script.
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"

# Install each node version...
IFS=',' read -r -a VERSIONLIST <<< "$${NODE_VERSIONS}"
for version in "$${VERSIONLIST[@]}"; do
if [ -z "$version" ]; then
continue
fi
printf "🛠️ Installing node version $${CODE}$version$${RESET}...\n"
output=$(nvm install "$version" 2>&1)
if [ $? -ne 0 ]; then
echo "Failed to install version: $version: $output"
exit 1
fi
done

# Set default if provided
if [ -n "$${DEFAULT}" ]; then
printf "🛠️ Setting default node version $${CODE}$DEFAULT$${RESET}...\n"
output=$(nvm alias default $DEFAULT 2>&1)
fi