Skip to content

Commit 8881c7f

Browse files
Improved handling of node installation (& miscellaneous improvements) (#1419)
* fix: Remove edison nodesource installation * fix: npm upgrade to latest *compatible* version, not the literal latest The previous command (which installs npm@latest) results in the installation of a version of npm which is almost certainly incompatible with the installed (regardless of source) version of nodejs * fix: Use n instead of apt for installing node & npm * fix: Improve node execution time check function - Remove what should be a redundant install, instead opt to fail if node is missing. Given that openaps-packages.sh handles installing node, this failing check indicates an unmitigated failure in the process previously, so let's not continue and make things potentially worse. - Attempt to remove existing nodejs/npm packages using apt - Warn the user if versions of node and npm remain - Switch nvm for n, using installed version of n if available Else, bootstrap install n as done elsewhere. * fix: Replace broken grep statements
1 parent 7d53991 commit 8881c7f

File tree

3 files changed

+52
-38
lines changed

3 files changed

+52
-38
lines changed

bin/openaps-install.sh

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,6 @@ if cat /etc/os-release | grep 'PRETTY_NAME="Debian GNU/Linux 8 (jessie)"' &> /de
4646
echo "Jubilinux 0.2.0, based on Debian Jessie, is no longer receiving security or software updates!"
4747
fi
4848

49-
#Workaround for Jubilinux to install nodejs/npm from nodesource
50-
if getent passwd edison &> /dev/null; then
51-
#Use nodesource setup script to add nodesource repository to sources.list.d
52-
curl -sL https://deb.nodesource.com/setup_8.x | bash -
53-
fi
54-
55-
#dpkg -P nodejs nodejs-dev
5649
# TODO: remove the `-o Acquire::ForceIPv4=true` once Debian's mirrors work reliably over IPv6
5750
apt-get -o Acquire::ForceIPv4=true update && apt-get -o Acquire::ForceIPv4=true -y dist-upgrade && apt-get -o Acquire::ForceIPv4=true -y autoremove
5851
apt-get -o Acquire::ForceIPv4=true update && apt-get -o Acquire::ForceIPv4=true install -y sudo strace tcpdump screen acpid vim locate ntpdate ntp

bin/openaps-packages.sh

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,20 @@ else
3030
sudo apt-get -y install jq || die "Couldn't install jq"
3131
fi
3232

33-
# Install/upgrade to latest version of node (v10) using apt if neither node 8 nor node 10+ LTS are installed
34-
if ! nodejs --version | grep -e 'v8\.' -e 'v1[02468]\.' &> /dev/null ; then
35-
if getent passwd edison; then
36-
# Only on the Edison, use nodesource setup script to add nodesource repository to sources.list.d, then install nodejs (npm is a part of the package)
37-
curl -sL https://deb.nodesource.com/setup_8.x | bash -
38-
sudo apt-get install -y nodejs=8.* || die "Couldn't install nodejs"
39-
else
40-
sudo apt-get install -y nodejs npm || die "Couldn't install nodejs and npm"
41-
fi
33+
# Install node using n if there is not an installed version of node >=8,<=19
34+
# Edge case: This is not likely to work as expected if there *is* a version of node installed, but it is outside of the specified version constraints
35+
if ! node --version | grep -q -e 'v[89]\.' -e 'v1[[:digit:]]\.'; then
36+
echo "Installing node via n..." # For context why we don't install using apt or nvm, see https://github.com/openaps/oref0/pull/1419
37+
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
38+
# Install the latest compatible version of node
39+
sudo bash n current
40+
# Delete the local n binary used to boostrap the install
41+
rm n
42+
# Install n globally
43+
sudo npm install -g n
4244

43-
# Upgrade npm to the latest version using its self-updater
44-
sudo npm install npm@latest -g || die "Couldn't update npm"
45+
# Upgrade to the latest supported version of npm for the current node version
46+
sudo npm upgrade -g npm|| die "Couldn't update npm"
4547

4648
## You may also need development tools to build native addons:
4749
## sudo apt-get install gcc g++ make

bin/oref0-setup.sh

Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,11 @@ function move_mmtune () {
292292
fi
293293
}
294294

295-
function install_or_upgrade_nodejs () {
296-
# install/upgrade to latest node 8 if neither node 8 nor node 10+ LTS are installed
297-
if ! nodejs --version | grep -e 'v8\.' -e 'v1[02468]\.' >/dev/null; then
298-
echo Installing node 8
299-
# Use nodesource setup script to add nodesource repository to sources.list.d
300-
sudo bash -c "curl -sL https://deb.nodesource.com/setup_8.x | bash -" || die "Couldn't setup node 8"
301-
# Install nodejs and npm from nodesource
302-
sudo apt-get install -y nodejs=8.* || die "Couldn't install nodejs"
295+
function check_nodejs_timing () {
296+
# Redundant check that node is installed
297+
# It is installed as part of openaps-packages.sh
298+
if ! node --version | grep -q -e 'v[89]\.' -e 'v1[[:digit:]]\.'; then
299+
die "No version of node (>=8,<=19) was found, which is an unexpected error (node installation should have been handled by previous installation steps)"
303300
fi
304301

305302
# Check that the nodejs you have installed is not broken. In particular, we're
@@ -314,17 +311,39 @@ function install_or_upgrade_nodejs () {
314311
echo "Your installed nodejs ($(node --version)) is very slow to start (took ${NODE_EXECUTION_TIME}s)"
315312
echo "This is a known problem with certain versions of Raspberry Pi OS."
316313

317-
if prompt_yn "Install a new nodejs version using nvm?" Y; then
318-
echo "Installing nvm and using it to replace the system-provided nodejs"
319-
320-
# Download nvm
321-
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
322-
# Run nvm, adding its aliases to this shell
323-
source ~/.nvm/nvm.sh
324-
# Use nvm to install nodejs
325-
nvm install 10.24.1
326-
# Symlink node into /usr/local/bin, where it will shadow /usr/bin/node
327-
ln -s ~/.nvm/versions/node/v10.24.1/bin/node /usr/local/bin/node
314+
if prompt_yn "Confirm installation of replacement nodejs/npm versions?" Y; then
315+
echo "Attempting to uninstall current nodejs/npm versions (apt-get remove)"
316+
sudo apt-get -y remove nodejs npm
317+
if [[ -n $NVM_DIR ]]; then
318+
echo "Removing nvm ($NVM_DIR)..."
319+
echo "(you may wish to optionally remove the nvm-related lines that still exist in ~/.bashrc; this script won't do it for you)"
320+
rm -rf "$NVM_DIR"
321+
fi
322+
323+
# Check that there node and npm are no longer available. If they are, warn the user.
324+
nodePath=$(command -v node)
325+
npmPath=$(command -v npm)
326+
if [[ -e "$nodePath" ]]; then
327+
echo "Note: A 'node' binary (located at '$nodePath') still exists and may interfere with the new installation of node"
328+
fi
329+
if [[ -e "$npmPath" ]]; then
330+
echo "Note: A 'npm' binary (located at '$npmPath') still exists and may interfere with the new installation of npm"
331+
fi
332+
333+
if [[ ! $(command -v n) ]]; then
334+
echo "n already exists on the system, using it to install a new version of node..."
335+
sudo n current
336+
else
337+
echo "Installing n and using it to replace the system-provided nodejs"
338+
echo "Installing node via n..."
339+
curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
340+
# Install the latest version of node that is supported on this platform
341+
sudo bash n current
342+
# Delete the local n binary used to boostrap the install
343+
rm n
344+
# Install n globally
345+
sudo npm install -g n
346+
fi
328347

329348
NEW_NODE_EXECUTION_TIME="$(\time --format %e node -e 'true' 2>&1)"
330349
echo "New nodejs took ${NEW_NODE_EXECUTION_TIME}s to start"
@@ -730,7 +749,7 @@ if prompt_yn "" N; then
730749
echo Running apt-get autoclean
731750
sudo apt-get autoclean
732751

733-
install_or_upgrade_nodejs
752+
check_nodejs_timing
734753

735754
# Attempting to remove git to make install --nogit by default for existing users
736755
echo Removing any existing git in $directory/.git

0 commit comments

Comments
 (0)