Skip to content

Instantly share code, notes, and snippets.

@MeLlamoPablo
Created February 1, 2017 11:34
Show Gist options
  • Save MeLlamoPablo/0abcc150c10911047fd9e5041b105c34 to your computer and use it in GitHub Desktop.
Save MeLlamoPablo/0abcc150c10911047fd9e5041b105c34 to your computer and use it in GitHub Desktop.
Creates a symlink to /usr/bin/node after using nvm
@fandredev
Copy link

thanks for this

@ramphy
Copy link

ramphy commented Sep 14, 2023

thank youu!

@geuis
Copy link

geuis commented Nov 4, 2023

The latest one linked sudo ln -s node /usr/bin can lead to recursive symlinks. However, using the original 4 liner at the beginning of the gist works reliably.

@adm-bome
Copy link

adm-bome commented May 27, 2024

SystemWide:

Create a userspace independend script:

sudo tee /usr/bin/link-current-nvm-version-to-system << FILE_CONTENT
#!/bin/bash

NVM_CURRENT_BIN="\$HOME/.nvm/current/bin"

if [ -e /usr/bin/node ]; then
  sudo rm -f /usr/bin/node
fi

if [ -e /usr/bin/npm ]; then
  sudo rm -f /usr/bin/npm
fi

sudo ln -s "\$NVM_CURRENT_BIN/node" /usr/bin/
sudo ln -s "\$NVM_CURRENT_BIN/npm" /usr/bin/
FILE_CONTENT

Create a sudoers file specific to linking: node and nvm

sudo tee /etc/sudoers.d/nvm-linking << FILE_CONTENT
%sudo ALL=(ALL:ALL) NOPASSWD: /usr/bin/rm -f /usr/bin/npm
%sudo ALL=(ALL:ALL) NOPASSWD: /usr/bin/rm -f /usr/bin/node
%sudo ALL=(ALL:ALL) NOPASSWD: /usr/bin/ln -s /home/*/npm /usr/bin/
%sudo ALL=(ALL:ALL) NOPASSWD: /usr/bin/ln -s /home/*/node /usr/bin/
FILE_CONTENT

Every User:

Add to User space: ~/.bashrc or ~/.zshrc

export NVM_SYMLINK_CURRENT=true
export NVM_DIR="$HOME/.nvm"
export NVM_CURRENT_BIN="$NVM_DIR/current/bin"

if [[ -s "$NVM_DIR/nvm.sh" ]]; then
    source "$NVM_DIR/nvm.sh"
fi

Create an autostart file when user logs into the system (repeat for every user)

tee ~/.config/autostart/link-current-nvm-to-system.desktop << FILE_CONTENT
[Desktop Entry]
Type=Application
Terminal=false
Name=Link Current NVM to System
Exec=/usr/sbin/link-current-nvm-version-to-system
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
FILE_CONTENT

Now you can switch node/npm versions as you like and the local node/npm wil always point to the Current active version

nvm install --lts
nvm ls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment