-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall-nodejs.sh
More file actions
executable file
·40 lines (33 loc) · 1.21 KB
/
Copy pathinstall-nodejs.sh
File metadata and controls
executable file
·40 lines (33 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/bin/bash
set -e
# Install Node.js via nvm (Node Version Manager).
# See https://github.com/nvm-sh/nvm
NVM_VERSION="${NVM_VERSION:-v0.40.4}"
NODE_VERSION="${NODE_VERSION:---lts}" # e.g. "22", "20.18.0", or "--lts"
# Install nvm via the official installer (idempotent — re-running upgrades).
export PROFILE=/dev/null # prevent the installer from editing rc files; we manage that below
curl -o- "https://raw.githubusercontent.com/nvm-sh/nvm/${NVM_VERSION}/install.sh" | bash
unset PROFILE
# Ensure nvm is initialized in ~/.bashrc
if ! grep -q 'NVM_DIR' "$HOME/.bashrc"; then
cat >> "$HOME/.bashrc" <<'EOF'
# nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
EOF
fi
# Make nvm available in this script for the install step.
export NVM_DIR="$HOME/.nvm"
\. "$NVM_DIR/nvm.sh"
nvm install "$NODE_VERSION"
# `nvm alias default` doesn't accept `--lts`; translate it to `lts/*`.
if [ "$NODE_VERSION" = "--lts" ]; then
nvm alias default 'lts/*'
else
nvm alias default "$NODE_VERSION"
fi
nvm use default
echo "Node installed: $(node --version)"
echo "npm: $(npm --version)"
echo "Restart your shell or run: source ~/.bashrc"